PHP解析xml格式数据工具类示例
作者:疯狂的代码-Fire 发布时间:2023-11-19 20:05:53
标签:PHP,解析xml
本文实例讲述了PHP解析xml格式数据工具类。分享给大家供大家参考,具体如下:
class ome_xml {
/**
* xml资源
*
* @var resource
* @see xml_parser_create()
*/
public $parser;
/**
* 资源编码
*
* @var string
*/
public $srcenc;
/**
* target encoding
*
* @var string
*/
public $dstenc;
/**
* the original struct
*
* @access private
* @var array
*/
public $_struct = array();
/**
* Constructor
*
* @access public
* @param mixed [$srcenc] source encoding
* @param mixed [$dstenc] target encoding
* @return void
* @since
*/
function SofeeXmlParser($srcenc = null, $dstenc = null) {
$this->srcenc = $srcenc;
$this->dstenc = $dstenc;
// initialize the variable.
$this->parser = null;
$this->_struct = array();
}
/**
* Parses the XML file
*
* @access public
* @param string [$file] the XML file name
* @return void
* @since
*/
function xml2array($file) {
//$this->SofeeXmlParser('utf-8');
$data = file_get_contents($file);
$this->parseString($data);
return $this->getTree();
}
function xml3array($file){
$data = file_get_contents($file);
$this->parseString($data);
return $this->_struct;
}
/**
* Parses a string.
*
* @access public
* @param string data XML data
* @return void
*/
function parseString($data) {
if ($this->srcenc === null) {
$this->parser = xml_parser_create();
} else {
if($this->parser = xml_parser_create($this->srcenc)) {
return '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); // lowercase tags
xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE, 1); // skip empty tags
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();
return false;
}
$this->_count = count($this->_struct);
$this->free();
}
/**
* return the data struction
*
* @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;
}
/**
* recursion the children node data
*
* @access public
* @param integer [$i] the last struct index
* @return array
*/
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;
}
/**
* Appends some values to an array
*
* @access public
* @param array [$target]
* @param string [$key]
* @param string [$value]
* @param array [$attributes]
* @param array [$inner] the children
* @return void
* @since
*/
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;
}
/**
* Free the resources
*
* @access public
* @return void
**/
function free() {
if (isset($this->parser) && is_resource($this->parser)) {
xml_parser_free($this->parser);
unset($this->parser);
}
}
}
希望本文所述对大家PHP程序设计有所帮助。
来源:http://blog.csdn.net/u013219814/article/details/68962653


猜你喜欢
- 最近想学习一些python数据分析的内容,就弄了个爬虫爬取了一些数据,并打算用Anaconda一套的工具(pandas, numpy, sc
- 1、重装后启动mysql服务,提示 本地计算机无法启动 mysql 服务 错误 1067:进程意外终止。2、查看mysql根目录下有一 计算
- 本文实例为大家分享了python os模块在系统管理中的应用代码,供大家参考,具体内容如下#临时文件import tempfile temp
- 引言近期网上这位卖蜂蜜的小伙鬼畜挺火的,大家质疑背景造假,这里我就带着大家实现“背景造假”(PS:原
- Bootstrap 通过一些简单的 HTML 标签和扩展的类即可创建出不同样式的表单。0x01 样式1一个登录界面:<!DOCTYPE
- 企业管理器中的Tools,Database Maintenance Planner,可以设置数据库的定期自动备份计划。并
- 一:工具准备Anaconda:是一个开源的Python发行版本,其中包含了conda、Python等180多个科学包及其依赖项。【Anaco
- 浏览器的具体功能都储存在服务器端的Browscap.ini中:<% SET
- 今儿继续再看老师给推荐的深入浅出mysql数据库开发这本书,看到innodb数据库的外键关联问题时,遇到了一个问题,书上写的是可以对父表进行
- 问题最近在研究图学习,在用networkx库绘图的时候发现问题。'''author:zhengtime:2020.1
- 什么是图像平滑处理在尽量保留图像原有信息的情况下,过滤掉图像内部的噪声,这一过程我们称之为图像的平滑处理,所得到的图像称为平滑图像。那么什么
- Python的3.0版本,常被称为Python 3000,或简称Py3k。相对于Python的早期版本,这是一个较大的升级。为了不带入过多的
- 1. 打开FrontPage 2003,点击“文件→新建→新建网站→其他网站模板”,然后选择“数据库界面向导”,给定网站路径后,单击[确定]
- 删除字段从Model中删除一个字段要比添加容易得多。 删除字段,仅仅只要以下几个步骤: 删除字段,然后重
- Embedding词嵌入在 pytorch 中非常简单,只需要调用 torch.nn.Embedding(m, n) 就可以了,m 表示单词
- Celery是一个异步的任务队列(也叫做分布式任务队列),一个简单,灵活,可靠的分布式系统,用于处理大量消息,同时为操作提供维护此类系统所需
- 前言可能很多人会觉得这是一个奇葩的需求,爬虫去好好的爬数据不就行了,解析js干嘛?吃饱了撑的?搜索一下互联网上关于这个问题还真不少,但是大多
- 先来一个官网链接:https://www.xmind.cn/有钱的也可以支持一波然后开始吧–百度网盘获取地址:链接: h
- 小的时候大家应该都玩过万花尺,将笔尖放置万花尺内不停的转动,便可以画出一幅精致的线稿图,现在我们不用万花尺,我们通过Python也能绘制出万
- 一 Protobuf介绍Google Protocol Buffer( 简称 Protobuf) 是 Google 公司内部的混合语言数据标