阅读量:0
SimpleXML在PHP中可以通过以下方法实现属性操作:
- 获取属性的值:
$xml = '<bookstore><book category="fiction">PHP Basics</book></bookstore>'; $books = simplexml_load_string($xml); $category = $books->book['category']; echo $category; // 输出: fiction
- 设置属性的值:
$xml = '<bookstore><book category="fiction">PHP Basics</book></bookstore>'; $books = simplexml_load_string($xml); $books->book['category'] = 'non-fiction'; echo $books->asXML(); // 输出: <bookstore><book category="non-fiction">PHP Basics</book></bookstore>
- 删除属性:
$xml = '<bookstore><book category="fiction">PHP Basics</book></bookstore>'; $books = simplexml_load_string($xml); unset($books->book['category']); echo $books->asXML(); // 输出: <bookstore><book>PHP Basics</book></bookstore>