HighDots Forums  

Reading a simple XML Document

Javascript JavaScript language (comp.lang.javascript)


Discuss Reading a simple XML Document in the Javascript forum.



Reply
 
Thread Tools Display Modes
  #1  
Old   
DougJrs
 
Posts: n/a

Default Reading a simple XML Document - 03-21-2008 , 05:45 PM






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");




Reply With Quote
  #2  
Old   
Thomas 'PointedEars' Lahn
 
Posts: n/a

Default Re: Reading a simple XML Document - 03-21-2008 , 05:55 PM






DougJrs wrote:
Quote:
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
--
Use any version of Microsoft Frontpage to create your site.
(This won't prevent people from viewing your source, but no one
will want to steal it.)
-- from <http://www.vortex-webdesign.com/help/hidesource.htm>


Reply With Quote
Reply




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off



Powered by vBulletin Version 3.5.4
Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.