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 7019155
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:08:47+00:00 2026-05-27T23:08:47+00:00

The XML parser of PHP calls the default handler function twice when it encounters

  • 0

The XML parser of PHP calls the default handler function twice when it encounters a special character in a string and therefore splits the string. I’ve tried to solve it using different encodings on the XML header as well in the PHP code, but it still splits the string:

$parser = xml_parser_create();
xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "ISO-8859-1");
xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);

xml_set_element_handler($parser, "startTag", "endTag");
xml_set_default_handler($parser, 'defaultHandler');


function startTag($p, $name, $attributes)
{

}

function endTag($p, $name)
{

}

function defaultHandler($parser, $data)
{
    if(strlen(trim($data)) > 0)
        echo '[' . $data . ']' . '<br />';
}                                                                                                                        

Example of the XML:

<variable name="GZH29" type="integer">
    <label>This is a small test with a special ë character. Let's try an ë character too</label>
</variable>

One would expect:

[This is a small test with a special ë character. Let's try an ë character too]

But the result is

[This is a small test with a special ]
[ë character. Let's try an ë character too]

I would like not to have the line splitted, so any idea what the solution is?

  • 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-27T23:08:48+00:00Added an answer on May 27, 2026 at 11:08 pm

    The xml_parser does create multiple events here for a reason I didn’t finally understood fully, I think this is because of the encoding auto-detection.

    You can deal with that by creating your own parser class. This is generally useful anyway, not only in this case. But for this case it’s especially so that you can put together the text of the label which get’s distributed over multiple events.

    The basic work is making the callback functions public function of a class, and register these functions then.

    Then each time the label tag opens, a temporary store is reset. When text appears, it’s added to that temporary store. If the label tag then closes, you can pass this text to a new “event” this time the function you’re looking for with it’s text:

    $variableParser = new VariableParser($parser);
    
    $file = 'data://,'.$xml;
    $fp = fopen($file, 'r');
    while(!feof($fp)) { $data = fread($fp, 4096); xml_parse($parser, $data, feof($fp)); }
    
    
    class VariableParser
    {
        private $label = ''; # place for the label text
        public function doLabel($text)
        {
            printf("[%s]<br />\n", $text);
        }
        public function __construct($parser = NULL)
        {
            if ($parser) $this->register($parser);
        }
        public function register($parser)
        {
            xml_set_element_handler($parser, array($this, "startTag"), array($this, "endTag"));
            xml_set_default_handler($parser, array($this, 'defaultHandler'));
        }
    
        public function startTag($parser, $name, $attributes)
        {
            if ($name === 'label') $this->label = '';
        }
    
        function endTag($parser, $name)
        {
            if ($name === 'label')
            {
                $this->doLabel($this->label);
                $this->label = '';
            }
        }
    
        function defaultHandler($parser, $data)
        {
            if(strlen(trim($data)) > 0)
            {
                $this->label .= $data;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have php function that parses a xml url and gives me an array.this
I'm writing a xml to text file script with PHP's xml parser. I delimit
Possible Duplicate: Best XML Parser for PHP -----First XML-----str1 <HOME> <USER_DETAIL> <LOCATION><![CDATA[MUMBAI]]></LOCATION> <NAME><![CDATA[RAVI]]></NAME> <ID><![CDATA[101]]></ID>
Possible Duplicate: Best XML Parser for PHP I'm submitting a form which returns an
i need to parse this php xml response in android: <?phpxml version=1.0 encoding=utf-8?> <SmsResponse>
I have an XML file which I need to parse using PHP and send
I've got a PHP page that parses an XML file with SimpleXml, then passes
I'm using a PHP script with SimpleXML to parse an XML feed. I have
I am using the XPath in PHP 5 to parse a XML document. The
I am beginner in PHP. I am trying to parse this xml file. <relationship>

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.