Solved

XML Response Parsing Issue

Posted on 2010-09-13
7
565 Views
Last Modified: 2012-05-10
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)?
0
Comment
Question by:cloud-9
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
7 Comments
 
LVL 82

Accepted Solution

by:
leakim971 earned 250 total points
ID: 33668358
the second one is not a valid XML content, it should be :


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

Open in new window

0
 
LVL 22

Expert Comment

by:kadaba
ID: 33668627
(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
0
 
LVL 4

Assisted Solution

by:MichaelMH
MichaelMH earned 250 total points
ID: 33669762
The XML sample presented by you is not valid one. In the first case, when you have only one element, that element is the root of your XML so that's why your code is working. But in the second case, when you have two elements on same level, the XML format is invalid since this means that you have two root nodes.
In this case you should wrap your XML content in a root node;

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

as leakim971 specified.
0
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 110

Expert Comment

by:Ray Paseur
ID: 33671690
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

0
 
LVL 110

Expert Comment

by:Ray Paseur
ID: 33681963
@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
0
 
LVL 82

Expert Comment

by:leakim971
ID: 33695595
Thanks for the points! Best Regards to Mr Ray Paseur!
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This article discusses how to create an extensible mechanism for linked drop downs.
This article demonstrates how to create a simple responsive confirmation dialog with Ok and Cancel buttons using HTML, CSS, jQuery and Promises
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question