天天看點

PHP XML解析類

PHP XML解析類

<?php   

/*  

  +----------------------------------------------------------------------+  

  | sofeeframework for php 4                                             |  

  | copyright (c) 2004-2005 sofee development team.(http://www.sofee.cn) |  

  | this source file is subject to version 1.00 of the sofee license,    |  

  | that is bundled with this package in the file license, and is        |  

  | available through the world-wide-web at the following url:           |  

  | http://www.sofee.cn/license/1_00.txt.                                |  

  | if you did not receive a copy of the sofee license and are unable to |  

  | obtain it through the world-wide-web, please send a note to          |  

  | [email protected] so we can mail you a copy immediately.              |  

  | author: justin wu <[email protected]>                              |  

*/   

/* $id: sofeexmlparser.php,v 1.3 2005/05/30 06:30:14 wenlong exp $ */   

/**  

* sofee xml parser class - this is an xml parser based on php's "xml" extension.  

*  

* the sofeexmlparser class provides a very simple and easily usable toolset to convert xml   

* to an array that can be processed with array iterators.  

* @package        sofeeframework  

* @access        public  

* @version        $revision: 1.1 $  

* @author        justin wu <[email protected]>  

* @homepage        http://www.sofee.cn  

* @copyright    copyright (c) 2004-2005 sofee development team.(http://www.sofee.cn)  

* @since        2005-05-30  

* @see            pear:xml_parser | simplexml extension  

class sofeexmlparser {   

    /**  

    * xml parser handle  

    *  

    * @var        resource  

    * @see        xml_parser_create()  

    */   

    var $parser;   

    * source encoding  

    * @var        string  

    var $srcenc;   

    * target encoding  

    var $dstenc;   

    * the original struct  

    * @access    private  

    * @var        array  

    var $_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();   

    }   

    * free the resources  

    **/   

    function free() {   

        if (isset($this->parser) && is_resource($this->parser)) {   

            xml_parser_free($this->parser);   

            unset($this->parser);   

        }   

    * parses the xml file  

    * @param        string        [$file] the xml file name  

    function parsefile($file) {   

        $data = @file_get_contents($file) or die("can't open file $file for reading!");   

        $this->parsestring($data);   

    * parses a string.  

    * @param        string        [$data] xml data  

    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);    // 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();   

            exit();   

        $this->_count = count($this->_struct);   

        $this->free();   

    * return the data struction  

    * @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  

    * @param        integer        [$i] the last struct index  

    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);   

                case 'cdata':   

                    // node has cdata after one of it's children   

                    $children['value'] .= $value;   

                case 'close':   

                    // end of node, return collected data    

                    return $children;   

            }   

        //return $children;   

    * appends some values to an array  

    * @param        array        [$target]  

    * @param        string        [$key]  

    * @param        string        [$value]  

    * @param        array        [$attributes]  

    * @param        array        [$inner] the children  

    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;   

            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]);   

                $target[$key][$index] = $child;   

                    $target[$key][$index][$k] = $v;   

            $target[$key][$index]['value'] = $value;   

        return $target;   

}  

?>  

 執行個體一 (解析xml檔案):

PHP XML解析類

<?php  

$file="http://dict.cn/ws.php?q=content";  

require_once('sofeexmlparser.php');  

$xml = new sofeexmlparser();  

$xml->parsefile($file);   

$tree = $xml->gettree();  

unset($xml);  

print "<pre>";  

foreach($tree as $val){  

    echo $val['key']['value'];  

    echo $val['lang']['value'];  

print "</pre>";  

執行個體二 (解析xml字元串):

PHP XML解析類

$str = '<?xml version="1.0" encoding="gb2312"?>  

<root>  

  <info value="adevy">  

    <name>adevy001</name>  

    <sex>男</sex>  

  </info>  

</root>';  

$xml->parsestring($str);  

print_r($tree);  

?>