首页 >php教程 >PHP源码 >PHP 往 XML 中添加节点

PHP 往 XML 中添加节点

PHP中文网
PHP中文网原创
2016-05-25 17:15:541015浏览

代码

<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 (&#39;contacts.xml&#39;);
     $contact_node = $doc->root ();
     $kids = $contact_node->children ();
     while ($node = array_shift ($kids)) {
          if (($node->type == XML_ELEMENT_NODE) && ($node->tagname == &#39;personal&#39;)) {
               $tel_node =& $node;
               break;
          }
     }
     if ($tel_node == null){
          die ("Couldn&#39;t locate &#39;personal&#39; node.");
     }
     $work_node = $tel_node->new_child (&#39;work&#39;, &#39;6666&#39;);
   
     print ("<pre class="brush:php;toolbar:false">\n");
     print (htmlspecialchars ($doc->dumpmem ()));
     print ("
\n"); ?>
声明:
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系admin@php.cn