首頁  >  文章  >  php教程  >  php中操作xml文档程序代码

php中操作xml文档程序代码

WBOY
WBOY原創
2016-05-25 16:42:101181瀏覽

下面我先是介绍一个朋友自己写的一个操作xml文档程序,然后再介绍了php中一个自带的解析xml文档的函数功能,有需要的朋友可参考,代码如下:

/* 

<?xml version="1.0" encoding="UTF-8" 
<班级> 
<学生 number="101"> 
<名字>孙悟空</名字> 
<名字>孙行者</名字> 
<年龄>猴精猴精</年龄> 
<介绍></介绍> 
</学生> 
<学生 number="102"> 
<名字>白骨精</名字> 
<年龄>140</年龄> 
<介绍>幻化万千</介绍> 
</学生> 
<学生 number="103"> 
<名字>猪八戒</名字> 
<名字>猪无能</名字> 
<年龄>200</年龄> 
<介绍>能吃会睡</介绍> 
</学生> 
</班级>

 

*/ 

<?php
class xmlDom{ 
    public $version; 
    public $encoding; 
    private $xml; 
    private $items; 
    private $seachNode = &#39;&#39;; 
    private $seachItem = &#39;&#39;; 
    private $seachValue = &#39;&#39;; 
    public $writeBytes = 0; 
 
    function __construct($xmlFile =&#39;&#39;, $version =&#39;1.0&#39;, $encoding = &#39;UTF-8&#39;){ 
        $this->version = $version; 
        $this->encoding = $encoding; 
        $this->xml = new DOMDocument($version, $encoding); 
        if($xmlFile)$this->xml->load($xmlFile); 
    } 
    function getRootEle($rootTag){ 
        $this->xmlRoot = $this->xml->getElementsByTagName($rootTag)->item(0); 
    } 
    function getSeachItem($itemsTag, $seachNode, $seachValue){ 
        $this->items = $this->xml->getElementsByTagName($itemsTag); 
        $this->items->length; 
        for($i=0; $i<$this->items->length; $i++){ 
            $item = $this->items->item($i);//元素 
            $node = $item->getElementsByTagName($seachNode);//节点 
            for($j = 0; $j< $node->length; $j++){ 
                $subNode = $node->item($j); 
                if($seachValue == $subNode->nodeValue){ 
                    $this->seachNode = $subNode; 
                    $this->seachItem = $item; 
                    $this->seachValue = $subNode->nodeValue; 
                    break(2); 
                } 
            } 
        } 
        return ($this->seachNode) ? true : false; 
    } 
     
    function update($nodeValue, $nodeTag = &#39;&#39;,$append = false, $index = 0){ 
        if($append){ 
            if($nodeTag) 
                $this->seachItem->getElementsByTagName($nodeTag)->item($index)->nodeValue += $nodeValue; 
            else 
                $this->seachNode->nodeValue += $nodeValue; 
        }else{ 
            if($nodeTag) 
                $this->seachItem->getElementsByTagName($nodeTag)->item($index)->nodeValue = $nodeValue; 
            else 
                $this->seachNode->nodeValue = $nodeValue; 
        } 
    } 
     
    function save($filename){ 
        $this->writeBytes = $this->xml->save($filename); 
        return ($this->writeBytes) ? true : false; 
    } 
} 
$test = new xmlDom(&#39;student.xml&#39;); 
$test->getSeachItem(&#39;学生&#39;,&#39;年龄&#39;,&#39;103&#39;);//找到 年龄=103 的猪八戒 
$test->update(&#39;小猪猪&#39;, &#39;名字&#39;, false, 1); //把猪八戒的第二个名字改成:小猪猪 
$test->save(&#39;new.xml&#39;); //保存成新文件
?>

上面是使用了dom来操作,下面我们利用php中的SimpleXML来操作xml,也算是很标准的一个操作xml文档的类了.

simplexml_load_file(string filename)

这里的 filename变量是用于存储 XML数据文件的文件名及其所在路径,以下代码使用 simplexml_load_file函数来创建了一个 SimpleXML对象,代码如下:

<?php  
$xml = simplexml_load_file(&#39;example.xml&#39;); //创建 SimpleXML对象  
print_r($xml);   //输出 XML  
?>

其中,example.xml存储的数据与上面的$data完全相同,运行结果也与上面完全相同.

上面两种方法实现了同样的功能,其区别就在于 XML的数据源不同,如果 XML的数据源在 PHP脚本文件中,则需要使用 simplexml_load_string来进行创建,如果 XML的数据源在一个单独的 XML文件中,则需要使用 simplexml_load_file来进行创建.

读取 XML数据中的标签

与操作数组类型的变量类似,读取 XML也可以通过类似的方法来完成,例如,如果需要读取上面 XML数据中每一个"depart"标签下的"name"属性,可以通过使用 foreach函数来完成,如以下代码所示.

<?php
$xml = simplexml_load_file(&#39;example.xml&#39;);
foreach($xml->depart as $a)  
{  
    echo "$a->name <BR>";  
}  
//运行结果如下所示。 
//production support 
//testing center 
?>

读取 XML文件,循环读取 XML数据中的每一个 depart标签,输出其中的 name属性,也可以使用方括号"[]"来直接读取 XML数据中指定的标签,以下代码输出了上面 XML数据中的第一个"depart"标签的"name"属性,代码如下:

<?php  
$xml = simplexml_load_file(&#39;example.xml&#39;); //读取 XML文件  
echo $xml->depart->name[0]; //输出节点  
   
//运行结果如下所示。 
//production support
?>

对于一个标签下的所有子标签,SimpleXML组件提供了 children方法进行读取,例如,对于上面的 XML数据中的"depart"标签,其下包括两个子标签:"name"和"employees",以下代码实现了对第一个"depart"标签下的子标签的读取.


文章链接:

随便收藏,请保留本文地址!

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn