PHP往XML中添加节点 PHP往XML中添加节点的方法
红薯 人气:0想了解PHP往XML中添加节点的方法的相关内容吗,红薯在本文为您仔细讲解PHP往XML中添加节点的相关知识和一些Code实例,欢迎阅读和指正,我们先划重点: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>
<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");
?>
$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");
?>
希望本文所述对大家的php程序设计有所帮助。
加载全部内容