欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  php教程

PHP 往 XML 中添加节点

程序员文章站 2022-06-17 18:06:15
...
代码
<contact id="43956">
     <personal>
          <name>
               <first>J</first>
               <middle>J</middle>
               <last>J</last>
          </name>
          <title>Manager</title>
          <employer>National</employer>
          <dob>1971-12-22</dob>
     </personal>
</contact>

2. php代码

<?php
     $tel_node = null;
     $doc = xmldocfile ('contacts.xml');
     $contact_node = $doc->root ();
     $kids = $contact_node->children ();
     while ($node = array_shift ($kids)) {
          if (($node->type == XML_ELEMENT_NODE) && ($node->tagname == 'personal')) {
               $tel_node =& $node;
               break;
          }
     }
     if ($tel_node == null){
          die ("Couldn't locate 'personal' node.");
     }
     $work_node = $tel_node->new_child ('work', '6666');
   
     print ("<pre>\n");
     print (htmlspecialchars ($doc->dumpmem ()));
     print ("</pre>\n");
?>