好得很程序员自学网

<tfoot draggable='sEl'></tfoot>

php中操作xml文档程序代码 - php高级应用

php中操作xml文档程序代码

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

/*   <?xml version="1.0" encoding="UTF-8"?>   <班级>   <学生 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;      }  }  //开源代码phpfensi.com   $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对象,代码如下:

<?php   $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函数来完成,如以下代码所示.

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

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

查看更多关于php中操作xml文档程序代码 - php高级应用的详细内容...

  阅读:42次