php解析xml数据格式,PHP解析xml格式数据工具类实例分享

本文主要介绍了PHP解析xml格式数据工具类,涉及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);

}

}

相关推荐:

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/469973.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

Linux命令入门

// 查看日历cal // 修改密码passwd // 查看目录和文件ls -lls // 查看当前用户信息whoami // 查看当前在线用户userswho 在Linux中&#xff0c;可以使用 vi 编辑器创建一个文本文件&#xff0c;例如&#xff1a;$ vi filename上面的命令会创建文件 filename 并打开&#xff0c;…

php 动态参数,php怎么实现动态传参数?

先贴代码&#xff0c;代码精简了。$invoker_function($argus);}}?>描述&#xff1a;程序是在ThinkPHP开发&#xff0c;目的是把Cache的get方法接收的参数转发到指定的方法上&#xff0c;最后一行&#xff1a;其中D方法是ThinkPHP自带的方法用的是单例模式。如果不加参数$ar…

Bug2算法的实现(RobotBASIC环境中仿真)

移动机器人智能的一个重要标志就是自主导航&#xff0c;而实现机器人自主导航有个基本要求——避障。之前简单介绍过Bug避障算法&#xff0c;但仅仅了解大致理论而不亲自动手实现一遍很难有深刻的印象&#xff0c;只能说似懂非懂。我不是天才&#xff0c;不能看几遍就理解理论中…

php l方法,ThinkPHP的L方法使用简介

thinkPHP的L方法用于启用多语言的情况下&#xff0c;设置和获取当前的语言定义。其调用格式为&#xff1a;L(语言变量[,语言值])1.设置语言变量除了使用语言包定义语言变量之外&#xff0c;我们可以用L方法动态设置语言变量&#xff0c;例如&#xff1a;L(LANG_VAR,语言定义);语…

linux-pcap 抓包程序框架

转&#xff1a;http://blog.chinaunix.net/uid-21556133-id-120228.html libpcap详解2010-12-01 22:07libpcap&#xff08;Packet Capture Library&#xff09;&#xff0c;即数据包捕获函数库&#xff0c;是Unix/Linux平台下的网络数据包捕获函数库。它是一个独立于系统的用户…

策略模式场景举例

容错恢复机制 容错恢复机制是应用程序开发中非常常见的功能。那么什么是容错恢复呢&#xff1f;简单点说就是&#xff1a;程序运行的时候&#xff0c;正常情况下应该按照某种方式来做&#xff0c;如果按照某种方式来做发生错误的话&#xff0c;系统并不会崩溃&#xff0…

php写抢票脚本,火车票抢票python代码公开揭秘!

市场上很多火车票抢票软件大家应该非常熟悉&#xff0c;但很少有人研究具体是怎么实现的&#xff0c;所以觉得很神秘&#xff0c;其实很简单。下面使用Python模拟抢票程序&#xff0c;给大家揭秘抢票到底是怎么回事。该代码仅供参考&#xff0c;主要用于大家沟通交流&#xff0…

python不用加号实现加法

问题: Calculate the sum of two integers a and b, but you are not allowed to use the operator and -.Example:Given a 1 and b 2, return 3. class Solution(object):def getSum(self, a, b):""":type a: int:type b: int:rtype: int"""…

.net与mysql,ASP.NET与MySql的连接

ASP.NET与MySql的连接1.数据连接方式ASP.NET本身的数据访问ADO.NET不支持对于MySql的连接和查询&#xff0c;但是MySQL官网上均提供了多种ASP.NET连接到MySQL的方式&#xff1a;(1)ODBC驱动的访问方式&#xff1a;mysql-connector-odbc-5.1.5-win32.msi&#xff0c;ODBC(2)ADO.…

php 反射 视频教程,php 实现反射

定义一个人类class person{public $name;public $gender;public function say(){//echo $this->name."say".$this->gender;}public function __set($name,$value){$this->name$value;}public function __get($name){if(!isset($name)){echo "未设置&qu…

代理模式——HeadFirst设计模式学习笔记

代理模式&#xff1a;为另一个对象提供一个替身或占位符控制这个对象的访问 特点&#xff1a; 让代理对象控制对象的访问&#xff0c;被代理对象可以是远程对象&#xff08;远程代理&#xff09;&#xff0c;创建开销较大对象&#xff08;虚拟代理&#xff09;&#xff0c;或需…

cursor用法java,Cursor的基本使用方法

Cursor的基本使用方法今天在用到Cursor的时候发现&#xff0c;有很多游标相关的知识还是有欠缺&#xff0c;在网上搜了篇基础讲解的文&#xff0c;觉得还不错&#xff0c;自己整理了一下发上来。虽然很基础&#xff0c;但是有一些内容之前确实没有很扎实得掌握&#xff0c;所以…

$(document).ready()与window.onload的区别

1、执行时间&#xff1a; window.onload要等到页面所有元素加载完毕才执行&#xff0c;包括&#xff08;图片、flash等&#xff09; $(document).ready()在DOM结构绘制完毕后就执行&#xff0c;不必等到加载完毕。 2、执行个数&#xff1a; 如果有多个window.onload&#xff0c…

mongo java 日期,Java 8日期/时间(JSR-310)类型与Spring Data MongoDB的映射

我有Java 8日期/时间字段的简单文档Documentpublic class Token {private Instant createdAt;...}我希望坚持使用Spring Data MongoDB 1.5版.但java.time.Instant类型的字段无法正确反序列化,因为MappingMongoConverter缺少java.time类的转换器.在Spring 4中,我找到了带有不同转…

win7(64位)php5.5-Apache2.4-mysql5.6环境安装

win7&#xff08;64位&#xff09;安装搭建 php-5.5.10 apache2.4.7 mysql-5.6.16 环境 工具/原料 php-5.5.10-Win32-VC11-x64.zip 下载地址: http://windows.php.net/download/ httpd-2.4.7-win64-VC11.zip 下载地址: http://www.apachelounge.com/download/ mysql-5.6.16-win…

matlab中计算不等式的解,大神们,求个解多元一次不等式的代码,要所有整数解...

该楼层疑似违规已被系统折叠 隐藏此楼查看此楼用lingo做的&#xff0c;我lingo特烂328*a1470*b1300*c570*d3750*e2080*f3900*g3070*h<9000;gin(a) ;gin(b) ;gin(c) ;gin(d) ;gin(e) ;gin(f) ;gin(g) ;gin(h) ;结果&#xff1a;Feasible solution found.Infeasibilities: 0.0…

14.5.5.1 An InnoDB Deadlock Example 一个InnoDB 死锁实例

14.5.5.1 An InnoDB Deadlock Example 一个InnoDB 死锁实例下面的例子演示了一个错误可以发生当一个lock 请求会导致一个死锁,例子设计2个客户端&#xff0c;A和B:Jekins:/root# mysql -uroot -p1234567 -e"SHOW ENGINE INNODB STATUS\G;" | grep -i dead Warning: …

php开发环境 ubuntu,Ubuntu配置PHP开发环境

开发环境安装目前web服务器有很多&#xff0c;本文安装Apache服务器&#xff1b;本文使用的服务器是Mysql服务器。sudo apt install apache2常用命令重启Apache&#xff1a;sudo /etc/init.d/apache2 restart重启php&#xff1a;sudo /etc/init.d/php-fapm restart配置apache服…

Jmeter java.lang.OutOfMemoryError: GC overhead limit exceeded

使用这个jmeter工具测试时&#xff0c;遇到这么个gc错误&#xff0c;网上找到了解决方案。原因是jmeter默认分配内存的参数很小&#xff0c;好像是256M吧。故而解决方法&#xff0c;就是增加内存&#xff1a; set HEAP-Xms4g -Xmx4gset NEW-XX:NewSize1g -XX:MaxNewSize1g 注意…

php基础知识总结大全,php基础知识回顾 —— 常量

您现在的位置是&#xff1a;网站首页>>PHP>>phpphp基础知识回顾 —— 常量发布时间&#xff1a;2019-01-23 17:23:08作者&#xff1a;wangjian浏览量&#xff1a;489点赞量&#xff1a;0在PHP中有这样一类变量&#xff0c;当变量值被定义之后&#xff0c;它的值就不…