Solved

XML Response Parsing Issue

Posted on 2010-09-13
7
552 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

OverviewThis article demonstrates a simple search form using AJAX. The purpose of the article is to demonstrate how to use the same code to render a page and javascript (JQuery) and AJAX to make subsequent calls to refine the results. The princip…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.
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 …

943 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

6 Experts available now in Live!

Get 1:1 Help Now