Home php教程 PHP源码 使用DOM复制(克隆)指定节点名数据到新的XML文件中

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

May 25, 2016 pm 05:13 PM

[代码][PHP]代码 

<?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>
*/
/*
使用DOM复制(克隆)指定节点名数据到新的XML文件中 ,用到三个类的相关知识点 : 
DOMDocument - DOMNodeList - DOMNode
1.DOMNodeList DOMDocument::getElementsByTagName ( string $name )
2.DOMNode DOMNodelist::item ( int $index )
3.DOMNode DOMNode::cloneNode ([ bool $deep ] )
*/
if(!function_exists(&#39;l&#39;)) {
    function l() {
        echo &#39;<br />********************************<br />&#39;;
    }
}
if(!function_exists(&#39;cp_xml&#39;)) {
    /* 
     * 复制指定节点元素信息到新XML文件中
     * @param  $dom : 源XML文件的DOM对象
     * @param  $newdom : 新XML文件的DOM对象
     * @param  $node: 指定复制的节点元素名
     * @param  $file: 新生成的XML文件名
     * @param  $attribute: 指定复制的节点元素的属性名
     * @return void
    */
    function cp_xml($dom,$newdom,$node,$file,$attribute = &#39;&#39;) {
        $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);  //debug
         
        $root = $newdom->createElement(&#39;root&#39;);
        $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)) {
                //创建属性名
                $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(&#39;1.0&#39;,&#39;utf-8&#39;);
    $dom->load("test10_12.xml");
 
    $node = &#39;content&#39;;
    $file = &#39;11_1.xml&#39;;
    cp_xml($dom,$newdom,$node,$file);
     
    //实例二
    $dom = new DOMDocument();
    $newdom = new DOMDocument(&#39;1.0&#39;,&#39;utf-8&#39;);
    $dom->load("test10_12.xml");
     
    $node = &#39;title&#39;;
    $file = &#39;11_2.xml&#39;;
    cp_xml($dom,$newdom,$node,$file,$attribute = &#39;name&#39;);
     
}
/*End of PHP*/
Copy after login


Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
4 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1669
14
PHP Tutorial
1273
29
C# Tutorial
1256
24