<?php
class xml {
var $tm = array();
var $xml_parser;
var $data = array();
var $element = ""; // 当前节点
var $stack = array(); // 缓存当前标头的相关参数
var $type; function trustedFile($file) {
// only trust local files owned by ourselves
if (!eregi("^([a-z]+)://", $file)
&& fileowner($file) == getmyuid()) {
return true;
}
return false;
} //处理元素的开始标头
function startElement($parser, $name, $attribs) {
if($this->element != "") {
array_push($this->stack,$this->element);
}
$this->element = array(Name => $name);
if(sizeof($attribs)) {
$this->element[Attribute] = $attribs;
}
} //处理元素的结束标头
function endElement($parser, $name) {
$element = array_pop($this->stack);
if(is_array($element)) {
$element[Element][] = $this->element;
$this->element = $element;
}else {
$this->data[Root] = $this->element;
$this->element = "";
}
} //处理字元资料标头
function characterData($parser, $data) {
$data = eregi_replace("^ +","",$data);
$data = eregi_replace("^\n+","",$data);
if(strlen($data) > 0) {
$this->element[Text] .= $data;
}
} function parse($data) {
$this->xml_parser = xml_parser_create();
xml_set_object($this->xml_parser, &$this); //使 XML 剖析器用对象 xml_parser_set_option($this->xml_parser, XML_OPTION_CASE_FOLDING, 1);
xml_set_element_handler($this->xml_parser, "startElement", "endElement");
xml_set_character_data_handler($this->xml_parser, "characterData");
$this->type = xml_parser_get_option($this->xml_parser, XML_OPTION_CASE_FOLDING);
if(is_file($data)) {
if(!($fp = @fopen($file, "r"))) {
echo "文件不存在";
return false;
}
while($data = fread($fp, 4096)) {
if(!xml_parse($this->xml_parser, $data, feof($fp))) {
printf("XML error: %s at line %d\n",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser));
return false;
}
}
}else {
if(!xml_parse($this->xml_parser, $data)) {
printf("XML error: %s at line %d\n",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser));
return false;
}
}
xml_parser_free($this->xml_parser);
return $this->data;
}
}
$xml = <<< EOD
<?xml version="1.0" encoding="UTF-8"?> <win>
<win id="1">10</win>
<win id="2">100</win>
<win id="3">1000</win>
<win id="4">10000</win>
</win>
EOD;$x = new xml;
$a = $x->parse($xml);
print_r($a);
?>
Array
(
[Root] => Array
(
[Name] => WIN
[Text] =>
[Element] => Array
(
[0] => Array
(
[Name] => WIN
[Attribute] => Array
(
[ID] => 1
) [Text] => 10
) [1] => Array
(
[Name] => WIN
[Attribute] => Array
(
[ID] => 2
) [Text] => 100
) [2] => Array
(
[Name] => WIN
[Attribute] => Array
(
[ID] => 3
) [Text] => 1000
) [3] => Array
(
[Name] => WIN
[Attribute] => Array
(
[ID] => 4
) [Text] => 10000
) ) ))呵呵,准备工作很多。还好是一次性的
class xml {
var $tm = array();
var $xml_parser;
var $data = array();
var $element = ""; // 当前节点
var $stack = array(); // 缓存当前标头的相关参数
var $type; function trustedFile($file) {
// only trust local files owned by ourselves
if (!eregi("^([a-z]+)://", $file)
&& fileowner($file) == getmyuid()) {
return true;
}
return false;
} //处理元素的开始标头
function startElement($parser, $name, $attribs) {
if($this->element != "") {
array_push($this->stack,$this->element);
}
$this->element = array(Name => $name);
if(sizeof($attribs)) {
$this->element[Attribute] = $attribs;
}
} //处理元素的结束标头
function endElement($parser, $name) {
$element = array_pop($this->stack);
if(is_array($element)) {
$element[Element][] = $this->element;
$this->element = $element;
}else {
$this->data[Root] = $this->element;
$this->element = "";
}
} //处理字元资料标头
function characterData($parser, $data) {
$data = eregi_replace("^ +","",$data);
$data = eregi_replace("^\n+","",$data);
if(strlen($data) > 0) {
$this->element[Text] .= $data;
}
} function parse($data) {
$this->xml_parser = xml_parser_create();
xml_set_object($this->xml_parser, &$this); //使 XML 剖析器用对象 xml_parser_set_option($this->xml_parser, XML_OPTION_CASE_FOLDING, 1);
xml_set_element_handler($this->xml_parser, "startElement", "endElement");
xml_set_character_data_handler($this->xml_parser, "characterData");
$this->type = xml_parser_get_option($this->xml_parser, XML_OPTION_CASE_FOLDING);
if(is_file($data)) {
if(!($fp = @fopen($file, "r"))) {
echo "文件不存在";
return false;
}
while($data = fread($fp, 4096)) {
if(!xml_parse($this->xml_parser, $data, feof($fp))) {
printf("XML error: %s at line %d\n",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser));
return false;
}
}
}else {
if(!xml_parse($this->xml_parser, $data)) {
printf("XML error: %s at line %d\n",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser));
return false;
}
}
xml_parser_free($this->xml_parser);
return $this->data;
}
}
$xml = <<< EOD
<?xml version="1.0" encoding="UTF-8"?> <win>
<win id="1">10</win>
<win id="2">100</win>
<win id="3">1000</win>
<win id="4">10000</win>
</win>
EOD;$x = new xml;
$a = $x->parse($xml);
print_r($a);
?>
Array
(
[Root] => Array
(
[Name] => WIN
[Text] =>
[Element] => Array
(
[0] => Array
(
[Name] => WIN
[Attribute] => Array
(
[ID] => 1
) [Text] => 10
) [1] => Array
(
[Name] => WIN
[Attribute] => Array
(
[ID] => 2
) [Text] => 100
) [2] => Array
(
[Name] => WIN
[Attribute] => Array
(
[ID] => 3
) [Text] => 1000
) [3] => Array
(
[Name] => WIN
[Attribute] => Array
(
[ID] => 4
) [Text] => 10000
) ) ))呵呵,准备工作很多。还好是一次性的
但是我现在不想让他们一次性都输出来,我怎样根据他的属性来显示他的数据,我怎样才能读某个标签的属性只显示它的值,比如我怎样读〈win id="2">,让100显示呢,别的都不显示。
switch $name{
case id:
global $id;
$id=1;
break;
}//判断是否读取的是id属性
在结束表头的函数里写上
switch $name{
case id:
global $id;
global $read;
$id=0;
$read=0;
break;
}//结束id属性的读取,并且结束读取
然后在处理内容的函数里写上
$global $id;
$global $read;
if($id==1){//说明现在处理的是id的属性值
if($data==2){//判断这个id的值是不是等于2,也可以用变量
$read=1;//表示已经找到所要的元素,开始读取
}
}
else{
if($read==1){//进行读取
echo $data."<br>";
}
}
case id:
global $id;
$id=1;
break;帮帮忙,能在详细点吗?
$a = $x->parse($xml);foreach($a['Root']['Element'] as $v) {
if($v['Attribute']['ID'] == 2) break;
}
echo $v['Text'];