程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> 網頁編程 >> PHP編程 >> PHP基礎知識 >> PHP生成xml 代碼

PHP生成xml 代碼

編輯:PHP基礎知識
 

<?php
/**
* @param array_change_xml.php
* @name 將數組生成為xml文檔
*/

class xml{
var $dbase; //數據庫,要讀取的XML文件
var $dbname; //數據庫名稱,頂層元素,與數據庫文件名稱一致
var $dbtable; //數據表,要取得的節點
var $parser; //剖析器
var $vals; //屬性
var $index; //索引
var $dbtable_array;//節點數組
var $array; //下級節點的數組
var $result; //返回的結果
var $querys;
function xml($dbase,$dbtable){
$this->dbase=$dbase;
$this->dbname="document";
$this->dbtable=$dbtable;
$data=$this->ReadXml($this->dbase);
if(!$data){
die( "無法讀取 $this->dbname.xml");
}
$this->parser = xml_parser_create();
xml_parser_set_option($this->parser,XML_OPTION_CASE_FOLDING,0);
xml_parser_set_option($this->parser,XML_OPTION_SKIP_WHITE,1);
xml_parse_into_struct($this->parser,$data,$this->vals,$this->index);
xml_parser_free($this->parser);
//遍歷索引,篩選出要取值的節點 節點名:$dbtable
foreach ($this->index as $key=>$val) {
if ($key == $this->dbtable) {
//取得節點數組
$this->dbtable_array = $val;
} else {
continue;
}
}
for ($i=0; $i < count($this->dbtable_array); $i+=2) {
$offset = $this->dbtable_array[$i] + 1;
$len = $this->dbtable_array[$i + 1] - $offset;
//array_slice() 返回根據 offset 和 length 參數所指定的 array 數組中的一段序列。
//所取節點下級數組
$value=array_slice($this->vals,$offset,$len);
//取得有效數組,合並為結果數組
$this->array[]=$this->parseEFF($value);
}
return true;
}
//將XML文件讀入並返回字符串
function ReadXml($file)
{
return file_get_contents($file);
}
//取得有效數組
function parseEFF($effective) {
for ($i=0; $i < count($effective); $i++){
$effect[$effective[$i][ "tag"]] = $effective[$i]["value"];
}
return $effect;
}
//xml_query(方法,條件,多條件時邏輯運算符and or or,總數據數組,插入或更新的數組)
function xml_query($method,$condition,$if='and',$array=array())
{
if(($method=='select')||($method=='count')){
return $this->xml_select($method,$condition,$if);
} elseif($method=='insert') {
return $this->xml_insert($condition,$if,$array);
} elseif($method=='update') {
return $this->xml_update($condition,$if,$array);
}
}
//取得xml數組
function xml_fetch_array($condition,$if)
{
//$this- >querys++;
$row = $this->array; //初始化數據數組
if($condition) {
//是否有條件,如有條件則生成符合條件的數組
//生成條件數組,條件格式 field,operator,match
$condition=explode( ",",$condition);//條件數組
$cs=count($condition)/3; //條件數
for($i=0;$i <$cs;$i++){
$conditions[]=array( "field"=>$condition[$i*3],"operator"=>$condition[$i*3+1],"match"=>$condition[$i*3+2]);
}
//echo count($row);
for($r=0;$r<count($row);$r++){
for($c=0;$c <$cs;$c++){
//$i++;
$condition=$conditions[$c]; //當前條件
$field=$condition['field']; //字段
$operator=$condition[ "operator"];//運算符
$match=$condition['match']; //匹配
if(($operator=='=') &&($row[$r][$field]==$match)){
$true++;//若條件符合,符合數加1
} elseif(($operator=='!=') &&($row[$r][$field]!=$match)){
$true++;//若條件符合,符合數加1
} elseif(($operator==' <')&&($row[$r][$field]<$match)){
$true++;//若條件符合,符合數加1
} elseif(($operator=='<=')&&($row[$r][$field]<=$match)){
$true++;//若條件符合,符合數加1
} elseif(($operator=='>')&&($row[$r][$field]>$match)){
$true++;//若條件符合,符合數加1
} elseif(($operator=='>')&&($row[$r][$field]>=$match)){
$true++;//若條件符合,符合數加1
}
}
//根據條件取值
if($if=='and'){
//如果多條件為and,當符合數等於條件數時,生成數組
if($true==$cs){
$result[]=$row[$r];
}
} else {
//如果多條件為or,當有符合紀錄時,生成數組
if($true!=0){
$result[]=$row[$r];
}
}
//echo $true;
//echo "<pre style=\"font-size:12px;\text-align:left\">";
//print_r($true);
$true=0;//符合條件數歸零,進入下一輪循環
}
} else {
$result=$this->array;
}
//echo "<pre style=\"font-size:12px;\text-align:left\">";
//print_r($this- >result);
return $result;
}

//篩選或統計
function xml_select($method,$condition,$if)  

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved