JS in the use of DOM replication (cloning) to specify the node naming data to the new XML file code _javascript tips

Source: Internet
Author: User
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 ');
}
?>

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.