?
Solved

XML Response Parsing Issue

Posted on 2010-09-13
7
Medium Priority
?
573 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 1000 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 1000 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
Video: Liquid Web Managed WordPress Comparisons

If you run run a WordPress, you understand the potential headaches you may face when updating your plugins and themes. Do you choose to update on the fly and risk taking down your site; or do you set up a staging, keep it in sync with your live site and use that to test updates?

 
LVL 111

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 111

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

Enroll in August's Course of the Month

August's CompTIA IT Fundamentals course includes 19 hours of basic computer principle modules and prepares you for the certification exam. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

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

I found this questions asking how to do this in many different forums, so I will describe here how to implement a solution using PHP and AJAX. The logical flow for the problem should be: Write an event handler for the first drop down box to get …
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
Suggested Courses

764 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