+ -
当前位置:首页 → 问答吧 → XML解析类

XML解析类

时间:2010-07-19

来源:互联网

声明:本类非本人原创,系他人基础上更改得来。不敢独享,拿来与大家一起分享。

/**
 * XML解析类
 *
 */
class XmlParse {
    
    /**
    * XML parser handle 句枘
    *
    * @var        resource
    * @see        xml_parser_create()的返回值
    */
    var $parser;
    /**
    * 字符集编码
    *
    * @var        string
    */
    var $srcenc;
    /**
    * 字符集解码
    *
    * @var        string
    */
    var $dstenc;
    /**
    * 结构的结果数组
    *
    * @access    private
    * @var        array
    */
    var $_struct = array();
    /**
    * Constructor
    *
    * @access       public
    * @param        mixed        [$srcenc] source encoding
    * @param        mixed        [$dstenc] target encoding
    * @return        void
    * @since        
    */
    function __construct($srcenc = null, $dstenc = null) {
        $this->srcenc = $srcenc;
        $this->dstenc = $dstenc;      
        $this->parser = null;
        $this->_struct = array();
    }
    /**
    * 释放结果集
    *
    * @access        public
    * @return        void
    **/
    function free() {
        if (isset($this->parser) && is_resource($this->parser)) {
            xml_parser_free($this->parser);
            unset($this->parser);
        }
    }
    /**
    * 处理XML文件类型的XML解析
    *
    * @access       public
    * @param        string        [$file] the XML file name
    * @return       void
    * @since        
    */
    function parseFile($file) {
     if(function_exists('file_get_contents')){
         $data = @file_get_contents($file) or die("Can't open file $file for reading!");
     }else{
      $handle = fopen($file,'r');
      $data = fread($handle,filesize($file));
     }
        $this->parseString($data);
    }
    /**
    * 解析XML字符串的方法
    *
    * @access       public
    * @param        string        [$data] XML data
    * @return       void
    */
    function parseString($data) {
        if ($this->srcenc === null) {
            $this->parser = @xml_parser_create() or die('Unable to create XML parser resource.');
        } else {
            $this->parser = @xml_parser_create($this->srcenc) or die('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);    // 取消大小写折叠
        xml_parser_set_option($this->parser, XML_OPTION_SKIP_WHITE, 1);        // 略过空白字符组成的值
        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();
            exit();
        }
        $this->_count = count($this->_struct); //统计解析的数量
        $this->free(); //释放资源
    }
    /**
    * 返回结果的树状结构
    *
    * @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);
    }
    /**
    * 获取子结点的值
    *
    * @access       private
    * @param        integer        [$i] the last struct index
    * @return       array
    */
    private 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;
    }
    /**
    * 向TREE中添加结点
    *
    * @access       private
    * @param        array        [$target]
    * @param        string        [$key]
    * @param        string        [$value]
    * @param        array        [$attributes]
    * @param        array        [$inner] the children
    * @return        void
    * @since        
    */
   private 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;
    }
   /**
    * 获取非树型数组
    */
    function getResult(){
     return $this->_struct;
    }
}


[ 此帖被cs123在2010-07-27 13:13重新编辑 ]

作者: cs123   发布时间: 2010-07-19

沙发先看下

作者: luwenjie   发布时间: 2010-07-19

顶了看看

作者: kadaseshima   发布时间: 2010-07-19

看看?

作者: hualiang928   发布时间: 2010-07-19

sagasfdsf

作者: luzhengfei   发布时间: 2010-07-19

值得吗?

作者: jiahaoaiyiyi   发布时间: 2010-07-19

看看,学习学习。

作者: rendi   发布时间: 2010-08-31

热门下载

更多