XML解析类
时间:2010-07-19
来源:互联网
/**
* XML解析类
*
*/
class XmlParse {
/**
* XML parser handle 句枘
*
* @var resource
* @see xml_parser_create()的返回值
*/
var $parser;
/**
* 字符集编码
*
* @var string
*/
var $srcenc;
/**
* 字符集解码
*
* @var string
*/
var $dstenc;
/**
* 结构的结果数组
*
* @access private
* @var array
*/
var $_struct = array();
/**
* Constructor
*
* @access public
* @param mixed [$srcenc] source encoding
* @param mixed [$dstenc] target encoding
* @return void
* @since
*/
function __construct($srcenc = null, $dstenc = null) {
$this->srcenc = $srcenc;
$this->dstenc = $dstenc;
$this->parser = null;
$this->_struct = array();
}
/**
* 释放结果集
*
* @access public
* @return void
**/
function free() {
if (isset($this->parser) && is_resource($this->parser)) {
xml_parser_free($this->parser);
unset($this->parser);
}
}
/**
* 处理XML文件类型的XML解析
*
* @access public
* @param string [$file] the XML file name
* @return void
* @since
*/
function parseFile($file) {
if(function_exists('file_get_contents')){
$data = @file_get_contents($file) or die("Can't open file $file for reading!");
}else{
$handle = fopen($file,'r');
$data = fread($handle,filesize($file));
}
$this->parseString($data);
}
/**
* 解析XML字符串的方法
*
* @access public
* @param string [$data] XML data
* @return void
*/
function parseString($data) {
if ($this->srcenc === null) {
$this->parser = @xml_parser_create() or die('Unable to create XML parser resource.');
} else {
$this->parser = @xml_parser_create($this->srcenc) or die('Unable to create XML parser resource with '. $this->srcenc .' encoding.');
}
if ($this->dstenc !== null) {
@xml_parser_set_option($this->parser, XML_OPTION_TARGET_ENCODING, $this->dstenc) or die('Invalid target encoding');
}
xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0); // 取消大小写折叠
xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE, 1); // 略过空白字符组成的值
if (!xml_parse_into_struct($this->parser, $data, &$this->_struct)) { // 将结果解析到结果集中
//出错信息
printf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($this->parser)),
xml_get_current_line_number($this->parser)
);
$this->free();
exit();
}
$this->_count = count($this->_struct); //统计解析的数量
$this->free(); //释放资源
}
/**
* 返回结果的树状结构
*
* @access public
* @return array
*/
function getTree() {
$i = 0;
$tree = array();
$tree = $this->addNode(
$tree,
$this->_struct[$i]['tag'],
(isset($this->_struct[$i]['value'])) ? $this->_struct[$i]['value'] : '',
(isset($this->_struct[$i]['attributes'])) ? $this->_struct[$i]['attributes'] : '',
$this->getChild($i)
);
unset($this->_struct);
return ($tree);
}
/**
* 获取子结点的值
*
* @access private
* @param integer [$i] the last struct index
* @return array
*/
private function getChild(&$i) {
// contain node data
$children = array();
// loop
while (++$i < $this->_count) {
// node tag name
$tagname = $this->_struct[$i]['tag'];
$value = isset($this->_struct[$i]['value']) ? $this->_struct[$i]['value'] : '';
$attributes = isset($this->_struct[$i]['attributes']) ? $this->_struct[$i]['attributes'] : '';
switch ($this->_struct[$i]['type']) {
case 'open':
// node has more children
$child = $this->getChild($i);
// append the children data to the current node
$children = $this->addNode($children, $tagname, $value, $attributes, $child);
break;
case 'complete':
// at end of current branch
$children = $this->addNode($children, $tagname, $value, $attributes);
break;
case 'cdata':
// node has CDATA after one of it's children
$children['value'] .= $value;
break;
case 'close':
// end of node, return collected data
return $children;
break;
}
}
//return $children;
}
/**
* 向TREE中添加结点
*
* @access private
* @param array [$target]
* @param string [$key]
* @param string [$value]
* @param array [$attributes]
* @param array [$inner] the children
* @return void
* @since
*/
private function addNode($target, $key, $value = '', $attributes = '', $child = '') {
if (!isset($target[$key]['value']) && !isset($target[$key][0])) {
if ($child != '') {
$target[$key] = $child;
}
if ($attributes != '') {
foreach ($attributes as $k => $v) {
$target[$key][$k] = $v;
}
}
$target[$key]['value'] = $value;
} else {
if (!isset($target[$key][0])) {
// is string or other
$oldvalue = $target[$key];
$target[$key] = array();
$target[$key][0] = $oldvalue;
$index = 1;
} else {
// is array
$index = count($target[$key]);
}
if ($child != '') {
$target[$key][$index] = $child;
}
if ($attributes != '') {
foreach ($attributes as $k => $v) {
$target[$key][$index][$k] = $v;
}
}
$target[$key][$index]['value'] = $value;
}
return $target;
}
/**
* 获取非树型数组
*/
function getResult(){
return $this->_struct;
}
}
作者: cs123 发布时间: 2010-07-19
作者: luwenjie 发布时间: 2010-07-19
作者: kadaseshima 发布时间: 2010-07-19
作者: hualiang928 发布时间: 2010-07-19
作者: luzhengfei 发布时间: 2010-07-19
作者: jiahaoaiyiyi 发布时间: 2010-07-19
作者: rendi 发布时间: 2010-08-31
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28