Home >Backend Development >PHP Tutorial >PHP modification code to add xml node attributes

PHP modification code to add xml node attributes

WBOY
WBOYOriginal
2016-07-25 09:04:17992browse
复制代码

2、php代码

  1. $dom=new DOMDocument('1.0');
  2. $dom->load('x.xml');
  3. $em=$dom->getElementsByTagName('emotions');
  4. $em=$em->item(0);
  5. $items=$em->getElementsByTagName('item');
  6. foreach($items as $a){
  7. foreach($a->attributes as $b){
  8. if($b->nodeValue=='Birthday'){
  9. $a->setAttribute('name','nBirthday');
  10. }
  11. }
  12. }
  13. $t=$dom->createElement('item');
  14. $t->setAttribute('name','x');
  15. $t->setAttribute('src','www.sohu.com');
  16. $t->setAttribute('duration','duration');
  17. $em->appendChild($t);
  18. $dom->save('x.xml');
  19. ?>
复制代码

脚本学堂 编辑推荐: php解析XML文档属性并编辑的代码 php xml文档解析函数学习实例 php解析XML数据的一段代码 PHP读取XML的几种方法 php中使用DOM类读取XML文件的代码 实例学习php操作XML的类DOMDocument



Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn