How PHP adds nodes to XML _php tips

Source: Internet
Author: User

The example in this article describes how PHP adds nodes to XML. Share to everyone for your reference. The specific methods are as follows:

1. Contacts.xml Code

Copy Code code as follows:
<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 code
Copy Code code as follows:
<?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");
?>

I hope this article will help you with your PHP program design.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.