Home > php教程 > php手册 > body text

PHP如何在XML中添加节点

WBOY
Release: 2018-09-28 10:30:17
Original
1091 people have browsed it

这篇文章主要介绍了PHP往XML中添加节点的方法,涉及php操作XML文件的技巧,具有一定参考借鉴价值,需要的朋友可以参考下

本文实例讲述了PHP往XML中添加节点的方法。分享给大家供大家参考。具体方法如下:

1. contacts.xml代码

代码如下:

<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>
Copy after login

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"); ?
Copy after login

Related labels:
php
source:php.cn
Statement of this Website
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
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!