Link to home
Start Free TrialLog in
Avatar of cloud-9
cloud-9

asked on

XML Response Parsing Issue

I am having an issue with reading the XML Response from my XML HTTP Requests. Whenever I only use a single element, like:

<response>text here</response>

I have no issues checking for it using xml.getElementsByTagName('response')[0]. But if I include another element, such as:

<response>text here</response>
<date>my date</date>

The check fails, and xml.getElementsByTagName('response')[0] returns null. I am echoing the xml with PHP using the herodoc syntax (echo<<<XML), could this be part of the problem (like whitespace or extra lines)?
ASKER CERTIFIED SOLUTION
Avatar of leakim971
leakim971
Flag of Guadeloupe image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
(Ideally for me even the first one is not, even though it will be correct to have a single node under the parent) The xml should always be wrapped under a root element.

 <?xml version="1.0"?>
<root>
<parent>
<child>
<subchild>
</subchild>
</child>
</parent
</root>

So it would be nice to have it say,
<respxml>
<response>
text here
</response>
</respxml>

So even if you add new child elements inside the respxml the xml will be valid and you would not run into trouble.

<respxml>
<response>
text here
</response>
<date>
my date
</date>
</respxml>

Best,
kadaba
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
PHP can help you understand this.  See the code snippet.  Outputs:

object(SimpleXMLElement)#1 (1) {
  [0]=>
  string(9) "text here"
}


Warning:  simplexml_load_string() [function.simplexml-load-string]: Entity: line 2: parser error : Extra content at the end of the document in /home/websitet/public_html/RAY_temp_cloud9.php on line 19
Warning:  simplexml_load_string() [function.simplexml-load-string]: <date>my date</date> in /home/websitet/public_html/RAY_temp_cloud9.php on line 19
Warning:  simplexml_load_string() [function.simplexml-load-string]: ^ in /home/websitet/public_html/RAY_temp_cloud9.php on line 19

bool(false)

<?php // RAY_temp_cloud9.php
error_reporting(E_ALL);
echo "<pre>";

// TEST DATA SETS
$xml = <<<XML
<response>text here</response>
XML;

$bad = <<<BAD
<response>text here</response>
<date>my date</date>
BAD;

// CONVERT THE XML STRINGS TO OBJECTS
$obj = SimpleXML_Load_String($xml);
var_dump($obj);

$obj = SimpleXML_Load_String($bad);
var_dump($obj);

Open in new window

@cloud-9: I think leakim971 deserves at least partial credit here, and maybe all the credit - that was the first correct answer you received and it was less than 30 minutes after asking the question.  You can use the "request attention" button to contact a moderator.  The moderator can tell you how to split the points among the answers.  Best regards, ~Ray
Thanks for the points! Best Regards to Mr Ray Paseur!