使用PHP 5.0 轻松解析XML文档(2)

2016-01-29 13:01 4 1 收藏

使用PHP 5.0 轻松解析XML文档(2),使用PHP 5.0 轻松解析XML文档(2)

【 tulaoshi.com - PHP 】

文件:SimpleDocumentParser.php

<?php/** *========================================================= * * @author     hahawen(大龄青年)   * @since      2004-12-04 * @copyright  Copyright (c) 2004, NxCoder Group * *========================================================= *//** * class SimpleDocumentParser * use SAX parse xml file, and build SimpleDocumentObject * all this pachage's is work for xml file, and method is action as DOM. * * @package SmartWeb.common.xml * @version 1.0 */class SimpleDocumentParser{ private $domRootObject = null; private $currentNO = null; private $currentName = null; private $currentValue = null; private $currentAttribute = null; public function getSimpleDocument() {     return $this->domRootObject; } public function parse($file) {        $xmlParser = xml_parser_create();     xml_parser_set_option($xmlParser,XML_OPTION_CASE_FOLDING, 0);     xml_parser_set_option($xmlParser,XML_OPTION_SKIP_WHITE, 1);     xml_parser_set_option($xmlParser, XML_OPTION_TARGET_ENCODING, 'UTF-8');     xml_set_object($xmlParser, $this);     xml_set_element_handler($xmlParser, "startElement", "endElement");     xml_set_character_data_handler($xmlParser, "characterData");        if (!xml_parse($xmlParser, file_get_contents($file)))            die(sprintf("XML error: %s at line %d", xml_error_string(xml_get_error_code($xmlParser)),
        xml_get_current_line_number($xmlParser))); xml_parser_free($xmlParser); } private function startElement($parser, $name, $attrs) { $this->currentName = $name; $this->currentAttribute = $attrs; if($this->currentNO == null) { $this->domRootObject = new SimpleDocumentRoot($name); $this->currentNO = $this->domRootObject; } else { $this->currentNO = $this->currentNO->createNode($name, $attrs); } } private function endElement($parser, $name) { if($this->currentName==$name) { $tag = $this->currentNO->getSeq(); $this->currentNO = $this->currentNO->getPNodeObject(); if($this->currentAttribute!=null && sizeof($this->currentAttribute)>0) $this->currentNO->setValue($name, array('value'=>$this->currentValue,
       'attrs'=>$this->currentAttribute)); else $this->currentNO->setValue($name, $this->currentValue); $this->currentNO->removeNode($tag); } else { $this->currentNO = (is_a($this->currentNO, 'SimpleDocumentRoot'))? null:
        $this->currentNO->getPNodeObject(); } } private function characterData($parser, $data) { $this->currentValue = iconv('UTF-8', 'GB2312', $data); } function __destruct() { unset($this->domRootObject); }}?>

  文件:SimpleDocumentBase.php

<?php/** *========================================================= * * @author     hahawen(大龄青年)   * @since      2004-12-04 * @copyright  Copyright (c) 2004, NxCoder Group * *========================================================= *//** * abstract class SimpleDocumentBase * base class for xml file parse * all this pachage's is work for xml file, and method is action as DOM. * * 1 add/update/remove data of xml file. * 2 explode data to array. * 3 rebuild xml file * * @package SmartWeb.common.xml * @abstract * @version 1.0 */abstract class SimpleDocumentBase{ private $nodeTag = null; private $attributes = array(); private $values = array(); private $nodes = array();    function __construct($nodeTag)    {        $this->nodeTag = $nodeTag;    }    pub
                        

来源:https://www.tulaoshi.com/n/20160129/1488603.html

延伸阅读
标签: PHP
  PHP包含支持Expat 解析器的可选XML扩展。PHP中的XML相关函数可以让你创建一个解析器来处理有效的XML文档。如果你所使用的Apache版本为1.3.7后的版本,则不用添加任何函数库。所需要做的只是使用--with-xml配置PHP。 在PHP手册中有好几个创建基于PHP的XML解析器很好的例子。此外,New Riders的Web Application Development with PHP 4....
标签: Web开发
在本文中主要就如何用SAX解析xml文档进行说明。 要解析的xml片段如下: <?xml version="1.0" encoding="UTF-8" standalone="yes"?> <Books> <Book id="8542f26f-80d4-4b7d-ab25-f80f72a852ef"> <name id="201"> <strings> <entry> <key>en_US</key> <value> thinking in Java</value> ...
标签: Web开发
/// summary /// author飞鹰@ASPCool.com /author /// description本文介绍在.NET中查找XML节点的方法。 /desciption /// copyrightASP酷技术资讯网(www.ASPCool.com) /copyright /// /summary 大家在.NET中处理XML文档的时候,经常会需要找到文档中的某个节点的数据。要找到某个节点,有许多种方法,在这里我就把几种常用...
标签: Web开发
XML在不同领域有着广泛的应用,比如在科技领域的MathML,无线通信应用的WML,在网络图象方面的SVG等等,我们这里侧重讨论XML在web上的应用。XML在web上应用主要是利用其强大的数据操作能力。一般用XML配合javascript和asp等服务器端程序,可以实现网络上几乎所有的应用需求。 考虑讲解方便,我们在下面介绍一个简单的实例,不包含服...
标签: Web开发
第五章:XML实例解析 提纲: 一:实例效果 二:实例解析 1.定义新标识。 2.建立XML文档。 3.建立相应的HTML文件。 XML在不同领域有着广泛的应用,比如在科技领域的MathML,无线通信应用的WML,在网络图象方面的SVG等等,我们这里侧重讨论XML在web上的应用。XML在web上应用主要是利用...

经验教程

940

收藏

64
微博分享 QQ分享 QQ空间 手机页面 收藏网站 回到头部