Copy Code code as follows:
<?php
/*
<?xml version= "1.0" encoding= "Utf-8"?>
<article>
<item>
<title name= "T1" ></title>
<content>content1</content>
<pubdate>2009-10-11</pubdate>
</item>
<item>
<title name= "T2" >title2</title>
<content>content2</content>
<pubdate>2009-11-11</pubdate>
</item>
</article>
*/
/*
Use DOM replication (cloning) to specify the node name data to a new XML file, using three classes of related knowledge points: Domdocument-domnodelist-domnode
1.DOMNodeList domdocument::getelementsbytagname (String $name)
2.DOMNode domnodelist::item (int $index)
3.DOMNode Domnode::clonenode ([bool $deep])
*/
if (!function_exists (' l ')) {
function L () {
Echo ' <br/>********************************<br/> ';
}
}
if (!function_exists (' Cp_xml ')) {
/*
* Copy the specified node element information into the new XML file
* @param $dom: Dom object for source XML file
* @param $newdom: DOM object for new XML file
* @param $node: Specifies the node element name for replication
* @param $file: Newly generated XML file name
* @param $attribute: Specifies the attribute name of the replicated node element
* @return void
*/
function Cp_xml ($dom, $newdom, $node, $file, $attribute = ') {
$contents = $dom->getelementsbytagname ($node);
$clone = Array ();
$attr = Array ();
for ($i = 0; $i < $contents->length; $i + +) {
$node = $contents->item ($i);
if ($node->hasattributes () &&!empty ($attribute)) {
$attr [] = $node->getattribute ($attribute);
}
$clone [] = $node->clonenode (true);
}
Var_dump ($ATTR);
$root = $newdom->createelement (' root ');
$newdom->appendchild ($root);
for ($i = 0; $i <count ($clone); $i + +) {
$title = $newdom->createelement ($clone [$i]->nodename, $clone [$i]->nodevalue);
$root->appendchild ($title);
if (count ($attr) >0 &&!empty ($attribute)) {
To create a property name
$aname = $newdom->createattribute ($attribute);
$title->appendchild ($aname);
Passing Property values
$aval = $newdom->createtextnode ($attr [$i]);
$aname->appendchild ($aval);
}
}
$newdom->save ($file);
}
}
if (file_exists ("Test10_12.xml")) {
Instance One
$dom = new DOMDocument ();
$newdom = new DOMDocument (' 1.0 ', ' utf-8 ');
$dom->load ("Test10_12.xml");
$node = ' content ';
$file = ' 11_1.xml ';
Cp_xml ($dom, $newdom, $node, $file);
Example Two
$dom = new DOMDocument ();
$newdom = new DOMDocument (' 1.0 ', ' utf-8 ');
$dom->load ("Test10_12.xml");
$node = ' title ';
$file = ' 11_2.xml ';
Cp_xml ($dom, $newdom, $node, $file, $attribute = ' name ');
}
?>