21,887
社区成员
发帖
与我相关
我的任务
分享
//建立根节点
$parent=new DOMDocument;
//建立节点的数组
$parent_Element_List=array('one,two,three,four,five,six,seven,eight,nine,ten');
$title_Elemnt_List=array('one,two,three,four,five,six,seven,eight,nine,ten');
$url_Elemnt_List=array('one,two,three,four,five,six,seven,eight,nine,ten');
$image_Elemnt_List=array('one,two,three,four,five,six,seven,eight,nine,ten');
//建立新的节点
for($i=0;$i<10;$i++)
{
//建立父节点
$parent_Elemnt_List[$i]=$parent->createElement('item');
//建立子节点
$title_Elemnt_List[$i]=$parent->createElement('title',rand(0,100));
$url_Elemnt_List[$i]=$parent->createElement('url',rand(30,100));
$image_Elemnt_List[$i]=$parent->createElement('image',rand(50,100));
//把子节点添加进父节点
$parent_Elemnt_List[$i]->appendChild($title_Elemnt_List[$i]);
$parent_Elemnt_List[$i]->appendChild($url_Elemnt_List[$i]);
$parent_Elemnt_List[$i]->appendChild($image_Elemnt_List[$i]);
//把父节点添加到根节点
$parent->appendChild($parent_Elemnt_List[$i]);
}
//加载xml
$dom = new DOMDocument();
$dom->load('fade.xml');
//查找需要替换的节点
$xpath=new DOMXPath($dom);
$nodelist=$xpath->query('/content/item');
$oldnode=array();
$newNodeList = $parent->getElementsByTagName('item');
for($i=0;$i<10;$i++)
{
$oldnode[$i] = $nodelist->item($i);
$newEntry = $newNodeList->item($i);
$newnode = $dom->importNode($newEntry,true);
$oldnode[$i]->parentNode->replaceChild($newnode,$oldnode[$i]);
}
echo $dom->save('ceshi2.xml');