php 操作xml类

Xml.class.php代码  

  1. <?php  
  2. /*****************************************  
  3.  * *  
  4.  * 文件名: xml.php                               
  5.  * 作  用: xml类,完善中,暂只支持三级节点                     
  6.  * *  
  7.  * 作  者: loking(biyees)                             
  8.  * Email:  biyees@gmail.com  QQ:4967530     
  9.  * *  
  10.  * example 读取数据:                                                                     
  11.  * $xml = new xml("dbase.xml",'table');             
  12.  * $data=$xml->xml_fetch_array();                   
  13.  * echo "<pre style=\"font-size:12px;\">";      
  14.  * print_r($data);                                    
  15.  * *  
  16.  简单说明  
  17. $xml=new xml('data.xml');  
  18. $array=$xml->xml_query('select','',,'');//select方法,取得数组  
  19. $result=$xml->xml_query(count,'',''');//count方法,取得统计数  
  20. $insert=$xml->xml_query('insert','','',$newarray);//insert方法,插入新数据(数组)  
  21. $update=$xml->xml_query('update','','',$newarray)//update方法,更新数据(数组)  
  22.  ********************************************/  
  23. class xml {  
  24.     var $dbase; //数据库,要读取的XML文件  
  25.     var $dbname; //数据库名称,顶层元素,与数据库文件名称一致  
  26.     var $dbtable; //数据表,要取得的节点  
  27.     var $parser; //剖析器  
  28.     var $vals; //属性  
  29.     var $index; //索引  
  30.     var $dbtable_array; //节点数组  
  31.     var $array; //下级节点的数组  
  32.     var $result; //返回的结果  
  33.     var $querys;  
  34.       
  35.     function xml($dbase, $dbtable) {  
  36.         $this->dbase = $dbase;  
  37.         $this->dbname = substr ( $dbase, strrpos ( $dbase, "/" ) + 1, - 4 );  
  38.         $this->dbtable = $dbtable;  
  39.         $data = $this->ReadXml ( $this->dbase );  
  40.         if (! $data) {  
  41.             die ( "无法读取 $this->dbname.xml" );  
  42.         }  
  43.         $this->parser = xml_parser_create ();  
  44.         xml_parser_set_option ( $this->parser, XML_OPTION_CASE_FOLDING, 0 );  
  45.         xml_parser_set_option ( $this->parser, XML_OPTION_SKIP_WHITE, 1 );  
  46.         xml_parse_into_struct ( $this->parser, $data, $this->vals, $this->index );  
  47.         xml_parser_free ( $this->parser );  
  48.         //遍历索引,筛选出要取值的节点 节点名:$dbtable  
  49.         foreach ( $this->index as $key => $val ) {  
  50.             if ($key == $this->dbtable) {  
  51.                 //取得节点数组  
  52.                 $this->dbtable_array = $val;  
  53.             } else {  
  54.                 continue;  
  55.             }  
  56.         }  
  57.         for($i = 0; $i < count ( $this->dbtable_array ); $i += 2) {  
  58.             $offset = $this->dbtable_array [$i] + 1;  
  59.             $len = $this->dbtable_array [$i + 1] - $offset;  
  60.             //array_slice() 返回根据 offset 和 length 参数所指定的 array 数组中的一段序列。  
  61.             //所取节点下级数组  
  62.             $value = array_slice ( $this->vals, $offset, $len );  
  63.             //取得有效数组,合并为结果数组  
  64.             $this->array [] = $this->parseEFF ( $value );  
  65.         }  
  66.         return true;  
  67.     }  
  68.     //将XML文件读入并返回字符串  
  69.     function ReadXml($file) {  
  70.         return file_get_contents ( $file );  
  71.     }  
  72.     //取得有效数组  
  73.     function parseEFF($effective) {  
  74.         for($i = 0; $i < count ( $effective ); $i ++) {  
  75.             $effect [$effective [$i] ["tag"]] = $effective [$i] ["value"];  
  76.         }  
  77.         return $effect;  
  78.     }  
  79.     //xml_query(方法,条件,多条件时逻辑运算符and or or,总数据数组,插入或更新的数组)  
  80.     function xml_query($method, $condition, $if = 'and', $array = array()) {  
  81.         if (($method == 'select') || ($method == 'count')) {  
  82.             return $this->xml_select ( $method, $condition, $if );  
  83.         } elseif ($method == 'insert') {  
  84.             return $this->xml_insert ( $condition, $if, $array );  
  85.         } elseif ($method == 'update') {  
  86.             return $this->xml_update ( $condition, $if, $array );  
  87.         }  
  88.     }  
  89.     //取得xml数组  
  90.     function xml_fetch_array($condition, $if) {  
  91.         //$this->querys++;  
  92.         $row = $this->array; //初始化数据数组  
  93.         if ($condition) {  
  94.             //是否有条件,如有条件则生成符合条件的数组  
  95.             //生成条件数组,条件格式 field,operator,match  
  96.             $condition = explode ( ",", $condition ); //条件数组  
  97.             $cs = count ( $condition ) / 3; //条件数  
  98.             for($i = 0; $i < $cs; $i ++) {  
  99.                 $conditions [] = array ("field" => $condition [$i * 3], "operator" => $condition [  
  100.   
  101.                 $i * 3 + 1], "match" => $condition [$i * 3 + 2] );  
  102.             }  
  103.             //echo count($row);  
  104.             for($r = 0; $r < count ( $row ); $r ++) {  
  105.                 for($c = 0; $c < $cs; $c ++) {  
  106.                     //$i++;  
  107.                     $condition = $conditions [$c]; //当前条件  
  108.                     $field = $condition ['field']; //字段  
  109.                     $operator = $condition ["operator"]; //运算符  
  110.                     $match = $condition ['match']; //匹配   
  111.                     if (($operator == '=') && ($row [$r] [$field] == $match)) {  
  112.                         $true ++; //若条件符合,符合数加1  
  113.                     } elseif (($operator == '!=') && ($row [$r] [$field] != $match)) {  
  114.                         $true ++; //若条件符合,符合数加1  
  115.                     } elseif (($operator == '<') && ($row [$r] [$field] < $match)) {  
  116.                         $true ++; //若条件符合,符合数加1  
  117.                     } elseif (($operator == '<=') && ($row [$r] [$field] <= $match)) {  
  118.                         $true ++; //若条件符合,符合数加1  
  119.                     } elseif (($operator == '>') && ($row [$r] [$field] > $match)) {  
  120.                         $true ++; //若条件符合,符合数加1  
  121.                     } elseif (($operator == '>') && ($row [$r] [$field] >= $match)) {  
  122.                         $true ++; //若条件符合,符合数加1  
  123.                     }  
  124.                 }  
  125.                 //根据条件取值  
  126.                 if ($if == 'and') {  
  127.                     //如果多条件为and,当符合数等于条件数时,生成数组  
  128.                     if ($true == $cs) {  
  129.                         $result [] = $row [$r];  
  130.                     }  
  131.                 } else {  
  132.                     //如果多条件为or,当有符合纪录时,生成数组  
  133.                     if ($true != 0) {  
  134.                         $result [] = $row [$r];  
  135.                     }  
  136.                 }  
  137.                 //echo $true;  
  138.                 //echo "<pre style=\"font-size:12px;\text-align:left\">";  
  139.                 //print_r($true);  
  140.                 $true = 0; //符合条件数归零,进入下一轮循环  
  141.             }  
  142.         } else {  
  143.             $result = $this->array;  
  144.         }  
  145.         //echo "<pre style=\"font-size:12px;\text-align:left\">";  
  146.         //print_r($this->result);  
  147.         return $result;  
  148.     }  
  149.     //筛选或统计  
  150.     function xml_select($method, $condition, $if) {  
  151.         $result = $this->xml_fetch_array ( $condition, $if );  
  152.         if ($method == 'select') {  
  153.             return $result;  
  154.         } else {  
  155.             return count ( $result );  
  156.         }  
  157.       
  158.     }  
  159.     //插入数据  
  160.     function xml_insert($condition, $if, $array) {  
  161.         $data = $this->xml_fetch_array ( $condition, $if ); //总数据数组  
  162.         $data [] = $array; //插入后的总数据数组  
  163.         $this->array = $data; //更新总数组  
  164.         $this->WriteXml ( $data );  
  165.     }  
  166.       
  167.     //得到更新的XML并改写  
  168.     function xml_update($condition, $if, $array) {  
  169.         $datas = $this->array; //总数据数组  
  170.         $subtract = $this->xml_fetch_array ( $condition, $if ); //要更新的数组  
  171.         //echo "<pre style=\"font-size:12px;\text-align:left\">";  
  172.         //print_r($data);  
  173.         //print_r($datas);  
  174.         //echo "每条记录中有".count($datas[0])."个值<br>";  
  175.         for($i = 0; $i < count ( $datas ); $i ++) {  
  176.             $data = $datas [$i];  
  177.             //echo "原始记录中的第".$i."条<br>";  
  178.             foreach ( $data as $k => $v ) {  
  179.                 //echo "-第".$i."条的".$k."值为".$v."<br>";  
  180.                 //echo "--要查找的数组".$k."值为".$subtract[0][$k]."<br>";  
  181.                 if ($v == $subtract [0] [$k]) {  
  182.                     $is ++;  
  183.                 }  
  184.             }  
  185.             if ($is == count ( $data )) {  
  186.                 //echo "----与第".$i."条符合<br>";  
  187.                 $datas [$i] = $array;  
  188.               
  189.         //array_splice($datas,$i,$i+1);  
  190.             }  
  191.             //echo "原始记录中的第".$i."条与要查找的有".$is."匹配<br>";    
  192.             //echo "原始记录中的第".$i."条结束<br>";  
  193.             $is = 0;  
  194.         }  
  195.         //array_splice($datas,2,2+1,$array);  
  196.         //echo "<pre style=\"font-size:12px;\text-align:left\">";  
  197.         //print_r($datas);  
  198.         $this->array = $datas;  
  199.         $this->WriteXml ( $datas );  
  200.       
  201.     }  
  202.     //写入XML文件(全部写入)  
  203.     function WriteXml($array) {  
  204.         if (! is_writeable ( $this->dbase )) {  
  205.             die ( "无法写入" . $this->dbname . ".xml" );  
  206.         }  
  207.         $xml .= "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";  
  208.         $xml .= "<$this->dbname>\r\n";  
  209.         for($i = 0; $i < count ( $array ); $i ++) {  
  210.             $xml .= "<$this->dbtable>\r\n";  
  211.             foreach ( $array [$i] as $k => $s ) {  
  212.                 $xml .= "<$k>$s</$k>\r\n";  
  213.             }  
  214.             $xml .= "</$this->dbtable>\r\n";  
  215.         }  
  216.         $xml .= "</$this->dbname>";  
  217.         $fp = @fopen ( $this->dbase, "w" );  
  218.         flock ( $fp, LOCK_EX );  
  219.         rewind ( $fp );  
  220.         fputs ( $fp, $xml );  
  221.         fclose ( $fp );  
  222.     }  
  223.     //逐行写入xml(我试着写入10000行,感觉没一次写入快,所以没用这种写入方式)  
  224.     function WriteLine($array) {  
  225.         if (! is_writeable ( $this->dbase )) {  
  226.             die ( "无法写入" . $this->dbname . ".xml" );  
  227.         }  
  228.         $fp = @fopen ( $this->dbase, "w" );  
  229.         rewind ( $fp );  
  230.         flock ( $fp, LOCK_EX );  
  231.         fputs ( $fp, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" );  
  232.         fputs ( $fp, "<$this->dbname>\r\n" );  
  233.         for($i = 0; $i < count ( $array ); $i ++) {  
  234.             fputs ( $fp, "<$this->dbtable>\r\n" );  
  235.             $xml .= "<$this->dbtable>\r\n";  
  236.             foreach ( $array [$i] as $k => $s ) {  
  237.                 fputs ( $fp, "<$k>$s</$k>\r\n" );  
  238.             }  
  239.             fputs ( $fp, "</$this->dbtable>\r\n" );  
  240.         }  
  241.         fputs ( $fp, "</$this->dbname>" );  
  242.         fclose ( $fp );  
  243.     }  
  244. }  
  245. ?>  

 

Java代码  

  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <ata>  
  3. <item>  
  4. <id>080808045206</id>  
  5. <title>XML标题11</title>  
  6. <text>PHP的XML类测试!22</text>  
  7. </item>  
  8. <item>  
  9. <id>080808045207</id>  
  10. <title>XML标题11</title>  
  11. <text>PHP的XML类测试!22</text>  
  12. </item>  
  13. <item>  
  14. <id>1234567</id>  
  15. <title>wangwang</title>  
  16. <text>wuying</text>  
  17. </item>  
  18. </ata>  

 使用方法:

Java代码  

  1. <?php  
  2. require_once('xml.class.php');  
  3. //xml_query(方法,条件,多条件时逻辑运算符and or or,总数据数组,插入或更新的数组)  
  4. //查询记录  
  5. $xml=new xml("data.xml","item");  
  6. $re=$xml->xml_query('select','','');  
  7. print_r($re);  
  8.   
  9. //记录数据数  
  10. /* 
  11. $xml=new xml("data.xml","item"); 
  12. $num=$xml->xml_query('count','title,=,XML标题11',''); 
  13. echo $num; 
  14. */  
  15.   
  16. //插入一条记录  
  17. /* 
  18. $xml = new xml("data.xml","item"); 
  19. $str=date('ymdhis'); 
  20. $newarray = array( 
  21. "id"=>"$str", 
  22. "title"=>"XML标33", 
  23. "text"=>"PHP的XML类测试!444" 
  24. ); 
  25. //第二及第三个变量位置是条件,留空表示在最后插入 
  26. $insert=$xml->xml_query('insert','','',$newarray); 
  27. */  
  28.    
  29. //修改记录  
  30. /* 
  31. $xml = new xml("data.xml","item"); 
  32. $array = array( 
  33. "id"=>"1234567", 
  34. "title"=>"wangwang", 
  35. "text"=>"wuying" 
  36. ); 
  37. //title标签等于xxx的用$array替换(可以建唯一属性的标签,比如id,这样就可以修改某一条记录) 
  38. $insert=$xml->xml_query('update','id,=,080808045227','and',$array); 
  39. */  
  40.   
  41. //删除记录  
  42. $xml = new xml("data.xml","item");  
  43. $array = array();  
  44. $insert=$xml->xml_query('update','id,=,080808045228','and',$array);//数组留空  
  45. ?>  

备注xml文件保存为utf8调用才可以正常

删除时其实是把值变空,我们可以修改一下xml_update(),在生成xml文件之前先判断$array的值,如果值为空就不写入到最终的数组中就是删除的效果了。

写入xml文件时速度粉快(我测试过30000条记录的情况),插入时只插入一条记录,修改速度也相当的快,挺适合中型网站生成XML时使用,所以推荐一下。

时间: 2024-09-12 08:11:59

php 操作xml类的相关文章

php操作xml类读取查询删除数据(支持三级节点)

文件名:xml.class.php  代码如下 复制代码 /*** * * * 文件名: xml.php * * 作 用: xml类,完善中,暂只支持三级节点 * * 作 者: loking(biyees) * * * * example 读取数据: * * $xml = new xml("dbase.xml",'table'); * * $data=$xml->xml_fetch_array(); * * echo "<pre style=\"font

一个通过DataSet操作XML的类(原创)

xml|原创 这段时间写的项目每次都要用到XML保存一些配置,而每次操作XML都觉得挺麻烦,没有数据库那么顺手.后来发现用DataSet操作XML很方便,而且灵活性比较好,于是写了一个操作XML的类,用来应付一般的XML操作(源码下载附件). 1 基本思路 其实用DataSet操作XML,归根到底就是对DataSet里的表格,行,列等进行操作,然后用DataSet里的东西重新写到XML中,从而实现编辑XML的目的.如果再配合上.xsd文件的话,那效果更佳. 2 程序详解 (1) XML文件内容

一个通过DataSet操作XML的类

xml 这段时间写的项目每次都要用到XML保存一些配置,而每次操作XML都觉得挺麻烦,没有数据库那么顺手.后来发现用DataSet操作XML很方便,而且灵活性比较好,于是写了一个操作XML的类,用来应付一般的XML操作(源码下载附件). 1 基本思路 其实用DataSet操作XML,归根到底就是对DataSet里的表格,行,列等进行操作,然后用DataSet里的东西重新写到XML中,从而实现编辑XML的目的.如果再配合上.xsd文件的话,那效果更佳. 2 程序详解 (1) XML文件内容 本类操

.NET操作XML完整类

using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.Web; namespace BLL { public class XmlDoc { /// <summary> /// 创建Xml文件 /// </summary> /// <param name="xmlPath">

Asp.Net+XML操作基类(修改,删除,新增,创建)第1/2页_实用技巧

/**********************************************************************************  *   * 功能说明:XML处理基类  * 作者: 刘功勋;  * 版本:V0.1(C#2.0);时间:2006-12-13  *   * *******************************************************************************/ using System;

PHP操作XML作为数据库的类_php技巧

xml.class.php文件代码 复制代码 代码如下: <?php * example 读取数据: * * $xml = new xml("dbase.xml",'table'); * * $data=$xml->xml_fetch_array(); * * echo "<pre style="font-size:12px;">"; * * print_r($data); * class xml { var $dbase

JDOM操作XML文件(法老修正版)

dom|xml 原文:JDOM操作XML文件地址:http://www.jspcn.net/htmlnews/200120272.html---------------------------------------------------------------------------    这篇文章讲的不错~把JDOM的基本操作实例化了,不过由于当时的作者用的JDOM版本还比较陈旧,所以部分代码会编译出错!支持中文也不好!所以,法老在原作者的基础上对程序做了修改!休正了编译出错以及中文乱码的

利用Visual Basic操作XML数据

visual|xml|数据 什么是XML 扩展标记语言XML是一种简单的数据存储语言,使用一系列简单的标记描述数据,而这些标记可以用方便的方式建立,虽然XML占用的空间比二进制数据要占用更多的空间,但XML极其简单易于掌握和使用. XML与Access,Oracle和SQL Server等数据库不同,数据库提供了更强有力的数据存储和分析能力,例如:数据索引.排序.查找.相关一致性等,XML仅仅是展示数据.事实上XML与其他数据表现形式最大的不同是:他极其简单.这是一个看上去有点琐细的优点,但正是

利用VisualBasic操作XML数据

visual|xml|数据 什么是XML 扩展标记语言XML是一种简单的数据存储语言,使用一系列简单的标记描述数据,而这些标记可以用方便的方式建立,虽然XML占用的空间比二进制数据要占用更多的空间,但XML极其简单易于掌握和使用. XML与Access,Oracle和SQL Server等数据库不同,数据库提供了更强有力的数据存储和分析能力,例如:数据索引.排序.查找.相关一致性等,XML仅仅是展示数据.事实上XML与其他数据表现形式最大的不同是:他极其简单.这是一个看上去有点琐细的优点,但正是