Reading a simple XML Document

D

DougJrs

Good Afternoon All,

I am trying to parse the XML below into three variables using the code
below, but it is not working. Can anyone help to identify what I am
doing wrong?

Thanks,
Doug

XML:
<?xml version="1.0"?>
<result>
<status>200</status>
<url>https://127.0.0.1/test</url>
<message>Success</message>
</result>


Javascript code:

var XMLresult = req.responseXML;
var Redirect = XMLresult.getElementsByTagName("url");
var status = XMLresult.getElementsByTagName("status");
var Message = XMLresult.getElementsByTagName("message");
 
T

Thomas 'PointedEars' Lahn

DougJrs said:
I am trying to parse the XML below into three variables using the code
below, but it is not working. Can anyone help to identify what I am
doing wrong?
[...]
XML:
<?xml version="1.0"?>
<result>
<status>200</status>
<url>https://127.0.0.1/test</url>
<message>Success</message>
</result>


Javascript code:

var XMLresult = req.responseXML;
var Redirect = XMLresult.getElementsByTagName("url");
var status = XMLresult.getElementsByTagName("status");
var Message = XMLresult.getElementsByTagName("message");

It is called getElement*s*ByTagName for a reason.

var Redirect = (XMLresult.getElementsByTagName("url") || [])[0];
var status = (XMLresult.getElementsByTagName("status") || [])[0];
var Message = (XMLresult.getElementsByTagName("message") || [])[0];

What matters is that you need to access the first (and only) element of the
returned NodeList. The `|| []' trick is only there to provide a value that
you can easily test against, which may be helpful in other situations.


PointedEars
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top