本文实例讲述了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);

}

}

}

PS:这里再为大家提供几款关于xml操作的在线工具供大家参考使用:

希望本文所述对大家PHP程序设计有所帮助。

php获得帮助类数据_PHP解析xml格式数据工具类示例相关推荐

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

    本文主要介绍了PHP解析xml格式数据工具类,涉及php针对xml格式数据节点添加.获取.解析等相关操作技巧,需要的朋友可以参考下,希望能帮助到大家. 本文实例讲述了PHP解析xml格式数据工具类.分 ...

  2. Android解析xml的方法,Android中解析XML格式数据的方法

    XML介绍:Extensible Markup Language,即可扩展标记语言 一.概述 Android中解析XML格式数据大致有三种方法: SAX DOM PULL 二.详解 2.1 SAX S ...

  3. iOS开发之解析XML格式数据

    XML格式的数据是一种数据的传输格式.因为它方便编写.结构清晰,所以深受程序猿的喜爱,非常多人都喜欢使用XML格式数据传输或者作为程序的配置信息. 如今我将来实如今iOS中解析XML格式数据,语言使用 ...

  4. 解析XML格式数据实例

    前言: 网络数据传输时,最常用的格式有两种:XML和JSON,下面我们就研究如何使用Pull解析和Sex解析来实现对XML格式文件数据的解析. 实例流程: 1.创建服务器:Apache服务器(用于模拟 ...

  5. Android初级开发(九)——网络交互—解析XML格式数据

    一.前言 首先我们先搭建一个web服务器,我这里用的是tomcat,搭建过程可参见tomacat服务器的搭建:http://blog.csdn.net/qq_28585471/article/deta ...

  6. java解析xml工具类_通过dom4j解析XML字符串XMLDocUtil工具类转换为XML文档及获取指定根节点及指定节点路径内容代码示例...

    一.前言 通过dom4j解析XML文档的XMLDocUtil工具类,进行解析xml字符串为Document文档对象.获取根节点元素路径内容getRootElement.获取唯一路径节点的值getSin ...

  7. 网络加载数据和解析JSON格式数据案例之空气质量监测应用

    一.创建一个新的项目 activity_main.xml <LinearLayout xmlns:android="http://schemas.android.com/apk/res ...

  8. Xml 格式数据的生成和解析

    相关阅读 XML约束 Xml 格式数据的生成和解析 XML解析器 什么是XML XML全称为Extensible Markup Language, 意思是可扩展的标记语言,它是 SGML(标准通用标记 ...

  9. Android——网络交互,如何调用接口并且json解析json格式和pull解析xml格式

    本菜鸟因为之前一直使用的是公司前辈封装好的类(网络接口调用超级方便有木有)完全没有想过有一天我会自己写,然后今天接触的时候一脸懵逼,然后今天花了一天时间终于研究好了:哈哈哈哈哈 二话不说先上代码: p ...

最新文章

  1. Eclipse中CVS版本管理
  2. 3. std::string::size_type
  3. 《数据科学R语言实践:面向计算推理与问题求解的案例研究法》一一2.3 数据清洗和变量格式化...
  4. C/C++ strlen函数为什么不能传入空指针NULL?
  5. 【KMP】重复子串(ybtoj KMP-2)
  6. 分享一个帮助你在线测试响应式设计的web工具 - Screenqueri.es
  7. 【转】Magento 2数据库EAV模型结构
  8. 百度SEO站长统计后台广告推送引流软件
  9. 乐高mindstormsev3_乐高MINDSTORMSEV3软件程序模块开发
  10. 解决Adobe reader闪退问题
  11. 卡尔曼滤波器和六轴传感器姿态融合资料整理
  12. 文件预览-使用SecretID和SecretKey进行腾讯云cos桶文件预览(word、ppt等)
  13. SpringCloud-狂神(1. 概述)学习笔记
  14. 机器人心灵感应:允许人类远程操作和训练机器人手
  15. CSS 之 后代、并列选择器
  16. SQL积累 计算相除之比+% ,转型,拼接, 多个左联,求和,统计,截取等
  17. 我想不通,MySQL 为什么使用 B+ 树来作索引?
  18. 第三方软件MOOS-IvP扩展
  19. java编程找出吸血鬼数字,Java 找到四位数的所有吸血鬼数字 基础代码实例
  20. DRILLNET 2.0------第十四章 钻具扭矩/摩阻模型

热门文章

  1. 与c交互_SV DPI-C接口学习心得
  2. 装oracle非常卡吗,win7系统安装完oracle电脑变卡的解决方法
  3. 动作分析 姿态估计_关于大片人物特效少不了的人体姿态估计,这里有一份综述文章...
  4. java冒泡排序_Java算法分析之冒泡排序(Bubble Sort)
  5. 遵义春季招生计算机学校,遵义计算机学校招生
  6. Java -cp 使用
  7. linux 普通用户touch权限不够_一篇文章让你轻松了解 Linux 的权限
  8. 新冠疫苗厂商科兴成立房地产公司?网友:准备打疫苗送房子了?
  9. Java前沿分享:value或许成为java的新关键字
  10. Jenkins Pipeline动态使用Git分支名称的技巧