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)?
<response>text here</response>
I have no issues checking for it using xml.getElementsByTagName('
<response>text here</response>
<date>my date</date>
The check fails, and xml.getElementsByTagName('
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
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-s tring]: 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-s tring]: <date>my date</date> in /home/websitet/public_html /RAY_temp_ cloud9.php on line 19
Warning: simplexml_load_string() [function.simplexml-load-s tring]: ^ in /home/websitet/public_html /RAY_temp_ cloud9.php on line 19
bool(false)
object(SimpleXMLElement)#1
[0]=>
string(9) "text here"
}
Warning: simplexml_load_string() [function.simplexml-load-s
Warning: simplexml_load_string() [function.simplexml-load-s
Warning: simplexml_load_string() [function.simplexml-load-s
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);
@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!
<?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