site Search:


 
    All Forums Hot Topics Gallery






how-to block ads


 
Search Topic:
Uniqs:
6621
Share Topic
Posting?
Post a:
Post a:
Links: ·How To Get Noticed ·Web Monks FAQ ·Webhosting FAQ ·Posting Code ·How To Post ·Webhosting forum
AuthorAll Replies


Entelligence
Ek Lite
Premium
join:2002-04-24
93103

1 edit

Javascript - parse xml string

I have found a decent amount of info on google - but nothing is seeming to work for me. I'm trying to take a javascript string, and parse it into an object so that I can traverse through it. Just as any other dom object, getElementById, firstChild, etc. Can anyone provide a simple example?

var xml_string = "<top id='first'><inner1>Inside one</inner1><inner2>Inside two</inner2></top>";
 
??? help here ??
 
alert(new_variable.getElementById('first').innerHTML);
 
 

I would like this to output
<inner1>Inside one</inner1><inner2>Inside two</inner2>
 

Thanks in advance.

--
If it were socially acceptable, I would drape myself in velvet.


theedj
Right Back At-Cha
Premium
join:2002-12-12
Calgary

Not sure 100% your end result, but using something like jQuery along with a great tutorial I found, I was able to do all sorts of stuff with a single XML file.
--
# a b c d e f g h i j k l m n o p q r s t u v w x y


muiredised
ESSE QUAM VIDERI

join:2007-06-11
Tacoma, WA
kudos:1

reply to Entelligence
This is very easy in Gecko based browsers because when you insert a string with innerHTML it is parsed and added into the DOM right away, and they have a non-standard method called DOMParser(). Internet Explorer however, is much more of a challenge. For a cross browser solution I found this:

»jmvidal.cse.sc.edu/talks/javascr···ides.xml

I was able to accomplish what you desire using sections 1.1 and 1.3 from the above link. I basically copied and pasted the code from those two sections, preceded it with "var XML = {};" and was able to access the contents of your xml_string via the DOM.

Beware of the differences between the HTML DOM and the XML DOM. More info on this can be found here (this article also features the same code as the above link):

»www.webreference.com/programming···2/4.html

Hope that helps.


Monday, 21-May 09:44:46 Terms of Use & Privacy | feedback | contact | Hosting by nac.net - DSL,Hosting & Co-lo
over 12.5 years online © 1999-2012 dslreports.com.
Most commented news this week
Hot Topics