PHP Document解析XML(对比基于事件xml解析)
2017-04-03 20:31
411 查看
<?php /** * Filename: Document.php * The description of the file: * ========================================= * Copy right 2017 * * ========================================= * Author: pengzhi * Version: 1.0.0 * Encoding:UTF-8 * Date: 2017/3/30 17:33 **/ class Document { private $document = array(); private $objDom = null; public function __construct() { $this->objDom = new DOMDocument(); } public function getError() { return ''; } public function serialize($data) { $arrResult = $this->parse($data); $str = '<?xml version="1.0" encoding="utf-8"?>'."\n<root>\n"; //内容输出 深度优先搜索 $str .= $this->dfs2($arrResult, 1); //echo '\n</root>'; $str .= "</root>"; //$str = ob_get_contents(); //ob_end_clean(); return $str; } /** * 解析xml 字符串为数组形式 返回 * @param $data * @return */ public function parse($data) { Bd_Log::debug("start parse ..."); if (!$this->objDom->loadXML($data)) { throw new RuntimeException("loadXML fail!"); } $root = $this->objDom->documentElement;//文档根元素documentElement $this->document = array(); if ($root) { $this->document[$root->localName] = $this->dfs($root); } return $this->document; } /** * 生成节点树 * @param $node * @return array */ public function dfs($node) { //echo XML_TEXT_NODE.PHP_EOL; 3 //echo $node->nodeName.PHP_EOL; if (XML_TEXT_NODE == $node->nodeType) { //XML_TEXT_NODE 3 return $node->nodeValue; } else { //XML_ELEMENT_NODE 1 $arr = array(); foreach ($node->childNodes as $idx => $subNode) { if (XML_TEXT_NODE == $subNode->nodeType && '#text' == $subNode->nodeName) { return $subNode->nodeValue; } if (1 < $node->getElementsByTagName($subNode->nodeName)->length) { //数组元素 $arr[$subNode->nodeName][] = $this->dfs($subNode); //属性处理 if ($subNode->attributes->length > 0) { $attrs = array(); foreach ($subNode->attributes as $attrName => $attrNode) { $attrs[$attrName] = $attrNode->nodeValue; } $l = count($arr[$subNode->nodeName]); $arr[$subNode->nodeName." attr"][$l-1] = $attrs; } } else { //普通元素 $arr[$subNode->nodeName] = $this->dfs($subNode); if ($subNode->attributes->length > 0) { $attrs = array(); foreach ($subNode->attributes as $attrName => $attrNode) { $attrs[$attrName] = $attrNode->nodeValue; } $arr[$subNode->nodeName." attr"]= $attrs; } } } return $arr; } } /** * @param array $arr * @return bool */ private function isAssoc(array $arr) { $keys = array_keys($arr); return $keys !== array_keys($keys); } /** * @param $item * @param int $level * @return string */ private function dfs2($item, $level = 1) { Bd_Log::trace(json_encode($item)."|level:{$level}|dfs"); $str = ''; if (!is_array($item)) { $str .= str_repeat('\t', $level).$item."\n"; } else { foreach ($item as $key => $val) { if (strpos($key, 'attr') > 0) { continue;//属性元素 跳过 } if (is_array($val) && !$this->isAssoc($val)) { //数组元素 foreach ($val as $idx => $subval) { $str .= str_repeat('\t', $level)."<{$key}"; //元素属性 todo if (isset($item["{$key} attr"][$idx])) { foreach ($item["{$key} attr"][$idx] as $k => $v) { $str .= " {$k}='{$v}'"; } } $str .=">\n"; //元素内容 $str .= $this->dfs2($subval, $level+1); $str .= str_repeat('\t', $level)."</{$key}>\n"; } } elseif(is_array($val)){ $str .= str_repeat('\t', $level)."<{$key}"; //元素属性 todo if (isset($item["{$key} attr"])) { foreach ($item["{$key} attr"] as $k => $v) { $str .= " {$k}='{$v}'"; } } $str .= ">\n"; $str .= $this->dfs2($val, $level+1); $str .= str_repeat('\t', $level)."</{$key}>\n"; } else { $str .= $this->dfs2($val, $level+1); }//if elseif else }//foreach }//else return $str; } }
相关文章推荐
- PHP基于事件的XML解析工具类
- [代码笔记] python 之 xml解析_sax:基于事件驱动的解析方式
- dom4j基于事件流解析大XML的文件
- 基于事件的xml文件解析(中国天气网xml解析)
- 基于事件的XML解析【使用解析器】(中国天气网)
- Php Xml解析之DOMDocument使用方法
- PHP基于DOMDocument解析和生成xml的方法分析
- php 输出xml错误不能解析,XML declaration allowed only at the start of the document
- php 基于事件的xml处理
- Php Xml解析之DOMDocument使用方法浅解
- 基于PHP与XML的PDF文档生成技术
- 传递: xml string到sql server时的sp_xml_preparedocument无法解析问题
- 基于事件驱动的解析接口 SAX (实例二)
- 基于事件驱动的解析接口 SAX (实例一)
- PHP 中的 XML 拉模式解析
- delphi中的XML解析控件TXMLDocument的用法
- 用PHP工具包expat解析XML
- php解析和编写xml
- PHP xml-rpc 应用说明-基于php扩展
- 基于PHP与XML的PDF文档生成技术