Solved

XML Response Parsing Issue

Posted on 2010-09-13
7
555 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
What is SQL Server and how does it work?

The purpose of this paper is to provide you background on SQL Server. It’s your self-study guide for learning fundamentals. It includes both the history of SQL and its technical basics. Concepts and definitions will form the solid foundation of your future DBA expertise.

 
LVL 109

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 109

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

PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

Question has a verified solution.

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

Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn the basics of jQuery, including how to invoke it on a web page. Reference your jQuery libraries: (CODE) Include your new external js/jQuery file: (CODE) Write your first lines of code to setup your site for jQuery.: (CODE)
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 …

777 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