温馨提示×

SimpleXML在php中如何实现属性操作

PHP
小樊
83
2024-08-15 06:04:36
栏目: 编程语言

SimpleXML在PHP中可以通过以下方法实现属性操作:

  1. 获取属性的值:
$xml = '<bookstore><book category="fiction">PHP Basics</book></bookstore>';
$books = simplexml_load_string($xml);
$category = $books->book['category'];
echo $category; // 输出: fiction
  1. 设置属性的值:
$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>
  1. 删除属性:
$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>

0