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
0
投稿
猜你喜欢
- 使用ajax获取服务器数据返回给客户端,出现中文乱码。在之前的一个ajax应用中指定codepage=936,将所有页面编码都指定为GB23
- --利用T-SQL语句,实现数据库的备份与还原的功能 ----体现了SQL Server中的四个知识点: ----1. 获取SQL Serv
- 没有多大变动,主要是返回错误信息,以便调用函数部分可以alert出来。据说可以用正则表达式校验,下次再研究下。//-------------
- 网站能切换几套CSS风格早已不是什么新鲜事了。大家也都知道怎么去弄。早上发现一个有意思得站点 http://www.leemunroe.co
- 1.linux下启动mysql的命令:mysqladmin start/ect/init.d/mysql start (前面为mysql的安
- 第一种:获取不带后缀的文件名,直接上代码:就是直接用basename()函数就可以返回路径中的文件名部分,其语法是“basename(pat
- 看到php的错误日志里有些这样的提示: [27-Aug-2011 22:26:12] PHP Warning: Cannot use a s
- 在标志设计中文字与图形元素总是紧密配合,只有恰当的字体与图形搭配时才能发挥最大的视觉效果。这也意味着你不能抛开图案,只是一厢情愿地选择你自己
- 集群是一种实现高可用性的有效解决方案,有时它会适得其反。而且,它还非常昂贵。因此,数据库管理员可使用日志转移代替集群来提供较高的可用性。日志
- 在网页中,我们经常需要引用大量的javascript和css文件,在加上许多javascript库都包含debug版和经过压缩的releas
- 对于更完整的代码可以参考,这个是支持数据库的版本。经过测试Asp+Ajax仿google搜索提示效果 数据库版google搜索提示.rar
- 介绍Trie树:又称为单词查找树,是一种树形结构,可以应用于统计字符串,会在搜索引擎系统中用于对文本的词频统计,下图是一个Trie树的结构,
- 下面说说主要实现思路: 1、存取图片 (1)、将图片文件转换为二进制并直接存进sql server //UploadHelper.cs //
- ThinkPHP支持多种php模板引擎,可以根据个人需要加以配置。下面我们以Smarty模板引擎为例,给大家说说具体的操作流程!首先去Sma
- Apple4.us的张亮问我:“很多人说用户体验是苹果的核心竞争力。在用户体验方面,我究竟该从苹果的设计中学习什么” ?关于这个问题的答案我
- 启动mysql server 失败,查看/var/log/mysqld.err080329 16:01:29 [ERROR] Can'
- rs.open sql,conn:如果sql是delete,update,insert则会返
- PHP PDO预定义常量以下常量由本扩展模块定义,因此只有在本扩展的模块被编译到PHP中,或者在运行时被动态加载后才有效。注意:PDO使用类
- 本文实例讲述了PHP自定义函数格式化json数据的方法。分享给大家供大家参考,具体如下:<?php /**
- 目前计算机中用得最广泛的字符集及其编码,是由美国国家标准局(ANSI)制定的ASCII码(American Stand ard C