一聚教程网:一个值得你收藏的教程网站

最新下载

热门教程

php中操作xml文档程序代码

时间:2013-03-12 编辑:简简单单 来源:一聚教程网

 代码如下 复制代码

 

/*

<班级>
<学生 number="101">
<名字>孙悟空
<名字>孙行者
<年龄>猴精猴精
<介绍>

<学生 number="102">
<名字>白骨精
<年龄>140
<介绍>幻化万千

<学生 number="103">
<名字>猪八戒
<名字>猪无能
<年龄>200
<介绍>能吃会睡


*/

class xmlDom{
    public $version;
    public $encoding;
    private $xml;
    private $items;
    private $seachNode = '';
    private $seachItem = '';
    private $seachValue = '';
    public $writeBytes = 0;

    function __construct($xmlFile ='', $version ='1.0', $encoding = 'UTF-8'){
        $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 = '',$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('student.xml');
$test->getSeachItem('学生','年龄','103');//找到 年龄=103 的猪八戒
$test->update('小猪猪', '名字', false, 1); //把猪八戒的第二个名字改成:小猪猪
$test->save('new.xml'); //保存成新文件

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

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

 代码如下 复制代码

$xml = simplexml_load_file(’example.xml’); //创建 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函数来完成,如以下代码
所示。

 代码如下 复制代码

depart as $a)
{
echo “$a->name
”;
}
?>

运行结果如下所示。
production support
testing center
//读取 XML文件 //循环读取 XML数据中的每一个 depart标签
//输出其中的 name属性
也可以使用方括号“ []”来直接读取 XML数据中指定的标签。以下代码输出了上面 XML数据中的第一个“depart”标签的“name”属性。

 

 代码如下 复制代码
$xml = simplexml_load_file(’example.xml’); //读取 XML文件
echo $xml->depart->name[0]; //输出节点
?>

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

热门栏目