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.