开发者

Reset XML Parser PHP

开发者 https://www.devze.com 2023-01-06 05:52 出处:网络
Hi I\'m using a class to parse my XML. I have to parse request XML and response XML, and I\'d like to do it with the same parser. When I parse the response XML, I keep getting junk after document ele

Hi I'm using a class to parse my XML.

I have to parse request XML and response XML, and I'd like to do it with the same parser. When I parse the response XML, I keep getting junk after document element error.

I narrowed my XML down to <?xml version="1.0" encoding="UTF-8" ?><check></check> and I'm still getting the error, so the only thing I could think of is the second <?xml ..?> header being parsed as 'junk'.

What I basically want to do is reset the XML parser so it can start off as a new document, but I don't want to create a new parser object. Is this possible?

Edit:

I'm using the following code in my XmlParser object.

<?php
    class XmlComponent extends Object {
        private $parser, $c, $current_tag;
        public $contents;

        function initialize(&$controller, $settings = array())
        {
            $this->controller =& $controller;
            $this->parser = xml_parser_create();
            xml_set_object($this->parser, $this);
            xml_set_element_handler($this->parser, "tag_open", "tag_close");
            xml_set_character_data_handler($this->parser, "cdata");
            xml_parser_set_option($this->parser, XML_OPTION_CASE_FOLDING, 0);

            if (isset($settings['xml'])) { 
                $contents = $set开发者_如何学Pythontings['xml'];
            }
            if (isset($settings['file'])) {
                $f = fopen($settings['file'], 'r');
                $contents = fread($f, filesize($settings['file']));
                fclose($f);
            }

            debug($this->parse($contents));

        }

        public function parse($xml)
        {

            $xml = trim(preg_replace("/>\s+</", '><', $xml));
            $this->contents = array();
            $this->c = &$this->contents;
            xml_parse($this->parser, $xml);
            return xml_error_string(xml_get_error_code($this->parser))."\r\n".$xml;
        }

        //*/
        public function xml($root)
        {
            $xml = '';
            foreach ($root as $tag=>$elem) {
                if (substr($tag, 0,1) != '@') {
                    foreach ($elem as $val) {
                        $xml .= '<'.$tag;
                        if (is_array($val) && isset($val['@attributes'])) {
                            foreach($val['@attributes'] as $a_key => $a_val) {
                                $xml .= ' '.$a_key.'="'.$a_val.'"';
                            }
                        }
                        $xml .= '>';
                        if (is_array($val) && isset($val['@data'])) {
                            $xml .= $val['@data'];
                        }
                        $xml .= $this->xml($val);
                        $xml .= '</'.$tag.'>';
                    }
                }
            }

            return $xml;
        }
        //*/

        private function tag_open($parser, $tag, $attributes)
        {
            if (!empty($attributes)) { $this->c[$tag][] = array('@attributes' => $attributes, '@parent' => &$this->c); }
            else { $this->c[$tag][] = array('@parent' => &$this->c); }

            $this->c =& $this->c[$tag][count($this->c[$tag]) - 1];

        }

        private function tag_close($parser, $tag)
        {
            $parent = &$this->c['@parent'];
            unset($this->c['@parent']);
            $this->c =& $parent;
        }

        private function cdata($parser, $data)
        {
            if (!empty($data)) {$this->c['@data'] = $data;}
        }
        //*/

    }
?>


What I basically want to do is reset the XML parser so it can start off as a new document, but I don't want to create a new parser object. Is this possible?

No, you have to create a new parser. You can, of course, provide in your class a method that "resets" the parser; i.e., deletes the current one and creates a new one, hence hiding it from the class consumer.

0

精彩评论

暂无评论...
验证码 换一张
取 消