栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > Web开发 > JavaScript

js中使用DOM复制(克隆)指定节点名数据到新的XML文件中的代码

JavaScript 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

js中使用DOM复制(克隆)指定节点名数据到新的XML文件中的代码

复制代码 代码如下:


if(!function_exists('l')) {
function l() {
echo '
********************************
';
}
}
if(!function_exists('cp_xml')) {

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$title = $newdom->createElement($clone[$i]->nodeName,$clone[$i]->nodevalue);
$root->appendChild($title);
if(count($attr)>0 && !empty($attribute)) {
//创建属性名
$aname = $newdom->createAttribute($attribute);
$title->appendChild($aname);
//传递属性值
$aval = $newdom->createTextNode($attr[$i]);
$aname->appendChild($aval);
}
}
$newdom->save($file);
}
}
if(file_exists("test10_12.xml")) {
//实例一
$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);
//实例二
$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');
}
?>
转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/113967.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号