Solved

XML Response Parsing Issue

Posted on 2010-09-13
7
551 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
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
IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 108

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 108

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

How to improve team productivity

Quip adds documents, spreadsheets, and tasklists to your Slack experience
- Elevate ideas to Quip docs
- Share Quip docs in Slack
- Get notified of changes to your docs
- Available on iOS/Android/Desktop/Web
- Online/Offline

Join & Write a Comment

Introduction Got endorsements from your clients?  Great!  There is almost nothing better than word-of-mouth advertising.  But how can you do that on the internet?  Sure you can make a page for endorsement quotations and list them all, but who is …
This article discusses four methods for overlaying images in a container on a web page
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

759 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

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now