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

avatar
作者
筋斗云
阅读量:0

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> 

广告一刻

为您即时展示最新活动产品广告消息,让您随时掌握产品活动新动态!