Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 907073
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 15, 20262026-05-15T16:29:36+00:00 2026-05-15T16:29:36+00:00

Hi I’m using a class to parse my XML. I have to parse request

  • 0

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 = $settings['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;}
        }
        //*/

    }
?>
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-15T16:29:36+00:00Added an answer on May 15, 2026 at 4:29 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I am trying to render a haml file in a javascript response like so:
I have a French site that I want to parse, but am running into
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
We are using XSLT to translate a RIXML file to XML. Our RIXML contains
I have thousands of HTML files to process using Groovy/Java and I need to
I have a view passing on information from a database: def serve_article(request, id): served_article
I have some data like this: 1 2 3 4 5 9 2 6
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.