PHP生成XML的类
作者:佚名 文章来源:网络转载 更新时间
:2007-11-26 23:33:06
$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==
【相关文章:】
php生成WAP页面
PHP生成动态WAP页面
浅淡PHP生成HTML的思路
用PHP生成自己的LOG文件
PHP生成便于打印的网页
PHP生成静态页面详解
用php生成EAN_13标准的条形码
PHP生成带有雪花背景的验证码
用ASP生成XBM数字图片(可用来生成验证码)
【发表评论】【打印此文】【关闭窗口】【点击数: 】
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==
【相关文章:】
php生成WAP页面
PHP生成动态WAP页面
浅淡PHP生成HTML的思路
用PHP生成自己的LOG文件
PHP生成便于打印的网页
PHP生成静态页面详解
用php生成EAN_13标准的条形码
PHP生成带有雪花背景的验证码
用ASP生成XBM数字图片(可用来生成验证码)
【发表评论】【打印此文】【关闭窗口】【点击数: 】
★好玩的休闲小游戏★
网友评论:
数据载入中,请稍后……
