免费注册 查看新帖 |

Chinaunix

  平台 论坛 博客 文库
最近访问板块 发新帖
查看: 1690 | 回复: 0
打印 上一主题 下一主题

php 操作xml类 [复制链接]

论坛徽章:
0
跳转到指定楼层
1 [收藏(0)] [报告]
发表于 2011-10-17 18:34 |只看该作者 |倒序浏览
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.                                 $i * 3 + 1], "match" => $condition [$i * 3 + 2] );
  101.                         }
  102.                         //echo count($row);
  103.                         for($r = 0; $r < count ( $row ); $r ++) {
  104.                                 for($c = 0; $c < $cs; $c ++) {
  105.                                         //$i++;
  106.                                         $condition = $conditions [$c]; //当前条件
  107.                                         $field = $condition ['field']; //字段
  108.                                         $operator = $condition ["operator"]; //运算符
  109.                                         $match = $condition ['match']; //匹配
  110.                                         if (($operator == '=') && ($row [$r] [$field] == $match)) {
  111.                                                 $true ++; //若条件符合,符合数加1
  112.                                         } elseif (($operator == '!=') && ($row [$r] [$field] != $match)) {
  113.                                                 $true ++; //若条件符合,符合数加1
  114.                                         } elseif (($operator == '<') && ($row [$r] [$field] < $match)) {
  115.                                                 $true ++; //若条件符合,符合数加1
  116.                                         } elseif (($operator == '<=') && ($row [$r] [$field] <= $match)) {
  117.                                                 $true ++; //若条件符合,符合数加1
  118.                                         } elseif (($operator == '>') && ($row [$r] [$field] > $match)) {
  119.                                                 $true ++; //若条件符合,符合数加1
  120.                                         } elseif (($operator == '>') && ($row [$r] [$field] >= $match)) {
  121.                                                 $true ++; //若条件符合,符合数加1
  122.                                         }
  123.                                 }
  124.                                 //根据条件取值
  125.                                 if ($if == 'and') {
  126.                                         //如果多条件为and,当符合数等于条件数时,生成数组
  127.                                         if ($true == $cs) {
  128.                                                 $result [] = $row [$r];
  129.                                         }
  130.                                 } else {
  131.                                         //如果多条件为or,当有符合纪录时,生成数组
  132.                                         if ($true != 0) {
  133.                                                 $result [] = $row [$r];
  134.                                         }
  135.                                 }
  136.                                 //echo $true;
  137.                                 //echo "<pre style=\"font-size:12px;\text-align:left\">";
  138.                                 //print_r($true);
  139.                                 $true = 0; //符合条件数归零,进入下一轮循环
  140.                         }
  141.                 } else {
  142.                         $result = $this->array;
  143.                 }
  144.                 //echo "<pre style=\"font-size:12px;\text-align:left\">";
  145.                 //print_r($this->result);
  146.                 return $result;
  147.         }
  148.         //筛选或统计
  149.         function xml_select($method, $condition, $if) {
  150.                 $result = $this->xml_fetch_array ( $condition, $if );
  151.                 if ($method == 'select') {
  152.                         return $result;
  153.                 } else {
  154.                         return count ( $result );
  155.                 }
  156.        
  157.         }
  158.         //插入数据
  159.         function xml_insert($condition, $if, $array) {
  160.                 $data = $this->xml_fetch_array ( $condition, $if ); //总数据数组
  161.                 $data [] = $array; //插入后的总数据数组
  162.                 $this->array = $data; //更新总数组
  163.                 $this->WriteXml ( $data );
  164.         }
  165.        
  166.         //得到更新的XML并改写
  167.         function xml_update($condition, $if, $array) {
  168.                 $datas = $this->array; //总数据数组
  169.                 $subtract = $this->xml_fetch_array ( $condition, $if ); //要更新的数组
  170.                 //echo "<pre style=\"font-size:12px;\text-align:left\">";
  171.                 //print_r($data);
  172.                 //print_r($datas);
  173.                 //echo "每条记录中有".count($datas[0])."个值<br>";
  174.                 for($i = 0; $i < count ( $datas ); $i ++) {
  175.                         $data = $datas [$i];
  176.                         //echo "原始记录中的第".$i."条<br>";
  177.                         foreach ( $data as $k => $v ) {
  178.                                 //echo "-第".$i."条的".$k."值为".$v."<br>";
  179.                                 //echo "--要查找的数组".$k."值为".$subtract[0][$k]."<br>";
  180.                                 if ($v == $subtract [0] [$k]) {
  181.                                         $is ++;
  182.                                 }
  183.                         }
  184.                         if ($is == count ( $data )) {
  185.                                 //echo "----与第".$i."条符合<br>";
  186.                                 $datas [$i] = $array;
  187.                        
  188.                 //array_splice($datas,$i,$i+1);
  189.                         }
  190.                         //echo "原始记录中的第".$i."条与要查找的有".$is."匹配<br>";  
  191.                         //echo "原始记录中的第".$i."条结束<br>";
  192.                         $is = 0;
  193.                 }
  194.                 //array_splice($datas,2,2+1,$array);
  195.                 //echo "<pre style=\"font-size:12px;\text-align:left\">";
  196.                 //print_r($datas);
  197.                 $this->array = $datas;
  198.                 $this->WriteXml ( $datas );
  199.        
  200.         }
  201.         //写入XML文件(全部写入)
  202.         function WriteXml($array) {
  203.                 if (! is_writeable ( $this->dbase )) {
  204.                         die ( "无法写入" . $this->dbname . ".xml" );
  205.                 }
  206.                 $xml .= "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n";
  207.                 $xml .= "<$this->dbname>\r\n";
  208.                 for($i = 0; $i < count ( $array ); $i ++) {
  209.                         $xml .= "<$this->dbtable>\r\n";
  210.                         foreach ( $array [$i] as $k => $s ) {
  211.                                 $xml .= "<$k>$s</$k>\r\n";
  212.                         }
  213.                         $xml .= "</$this->dbtable>\r\n";
  214.                 }
  215.                 $xml .= "</$this->dbname>";
  216.                 $fp = @fopen ( $this->dbase, "w" );
  217.                 flock ( $fp, LOCK_EX );
  218.                 rewind ( $fp );
  219.                 fputs ( $fp, $xml );
  220.                 fclose ( $fp );
  221.         }
  222.         //逐行写入xml(我试着写入10000行,感觉没一次写入快,所以没用这种写入方式)
  223.         function WriteLine($array) {
  224.                 if (! is_writeable ( $this->dbase )) {
  225.                         die ( "无法写入" . $this->dbname . ".xml" );
  226.                 }
  227.                 $fp = @fopen ( $this->dbase, "w" );
  228.                 rewind ( $fp );
  229.                 flock ( $fp, LOCK_EX );
  230.                 fputs ( $fp, "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n" );
  231.                 fputs ( $fp, "<$this->dbname>\r\n" );
  232.                 for($i = 0; $i < count ( $array ); $i ++) {
  233.                         fputs ( $fp, "<$this->dbtable>\r\n" );
  234.                         $xml .= "<$this->dbtable>\r\n";
  235.                         foreach ( $array [$i] as $k => $s ) {
  236.                                 fputs ( $fp, "<$k>$s</$k>\r\n" );
  237.                         }
  238.                         fputs ( $fp, "</$this->dbtable>\r\n" );
  239.                 }
  240.                 fputs ( $fp, "</$this->dbname>" );
  241.                 fclose ( $fp );
  242.         }
  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. $xml=new xml("data.xml","item");
  11. $num=$xml->xml_query('count','title,=,XML标题11','');
  12. echo $num;
  13. */

  14. //插入一条记录
  15. /*
  16. $xml = new xml("data.xml","item");
  17. $str=date('ymdhis');
  18. $newarray = array(
  19. "id"=>"$str",
  20. "title"=>"XML标33",
  21. "text"=>"PHP的XML类测试!444"
  22. );
  23. //第二及第三个变量位置是条件,留空表示在最后插入
  24. $insert=$xml->xml_query('insert','','',$newarray);
  25. */

  26. //修改记录
  27. /*
  28. $xml = new xml("data.xml","item");
  29. $array = array(
  30. "id"=>"1234567",
  31. "title"=>"wangwang",
  32. "text"=>"wuying"
  33. );
  34. //title标签等于xxx的用$array替换(可以建唯一属性的标签,比如id,这样就可以修改某一条记录)
  35. $insert=$xml->xml_query('update','id,=,080808045227','and',$array);
  36. */

  37. //删除记录
  38. $xml = new xml("data.xml","item");
  39. $array = array();
  40. $insert=$xml->xml_query('update','id,=,080808045228','and',$array);//数组留空
  41. ?>备注
复制代码
删除时其实是把值变空,我们可以修改一下xml_update(),在生成xml文件之前先判断$array的值,如果值为空就不写入到最终的数组中就是删除的效果了。

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

本版积分规则 发表回复

  

北京盛拓优讯信息技术有限公司. 版权所有 京ICP备16024965号-6 北京市公安局海淀分局网监中心备案编号:11010802020122 niuxiaotong@pcpop.com 17352615567
未成年举报专区
中国互联网协会会员  联系我们:huangweiwei@itpub.net
感谢所有关心和支持过ChinaUnix的朋友们 转载本站内容请注明原作者名及出处

清除 Cookies - ChinaUnix - Archiver - WAP - TOP