how to parse xml from javascript string?

  • Thread starter aaronwmail-usenet
  • Start date
A

aaronwmail-usenet

Ok, I've seen many examples of javascript
parsing an XML document retrieved from a file
indicated by a URL.

The problem is I need to parse (usually small)
xml *strings* embedded in the javascript program
or generated locally, and I can't find any way
to do this exposed (even though some internal
part of the engine clearly is capable of doing it).
Am I missing something?

I'm aquainted with Xparse, but it seems to do
strange things with invalid XML
(rather than giving up with an error) and also may
have licensing issues for my purposes. I've seen
references to other parsers implemented in
javascript. Is there a good one with a BSD style
license? Other options?

Please inform! thanks! -- Aaron Watters

===
there ain't no sanity clause!
 
M

Martin Honnen

The problem is I need to parse (usually small)
xml *strings* embedded in the javascript program
or generated locally, and I can't find any way
to do this exposed (even though some internal
part of the engine clearly is capable of doing it).

The JavaScript engine is capable of parsing XML? Well, yes, the
Spidermonkey engine in Firefox 1.5 can do that but that does not help
currently on the web.
Usually you need a host object for that, Mozilla exposes DOMParser e.g.
var domParser = new DOMParser();
var xmlDocument = domParser.parseFromString(
stringWithXMLMarkupHere,
'application/xml'
);
Opera 8 also supports DOMParser and has some minimal support for W3C DOM
Level 3 Load and Save too.
I think Safari 2 or at least the latest 2.something release also
supports DOMParser.
With IE/Win for XML you need to use MSXML anyway and there DOMDocument
exposes a method named loadXML to parse a string and create an XML DOM
document.

For older Safari (and probably Konqueror too) people have managed to
parse XML strings by passing the XML markup in a data URL to
XMLHttpRequest, see <http://web-graphics.com/mtarchive/001606.php>
 
A

aaronwmail-usenet

Thanks very much!

Why is it that my 4 recent large books on javascript don't
have this kind of information? Where is an authoritative
reference (online or otherwise) where I can find this kind
of info? Thanks again!

-- Aaron Watters
===
He's a humble man with lots to be humble about.
-- Churchill
 
T

Thomas 'PointedEars' Lahn

Why is it that my 4 recent large books on javascript don't
have this kind of information?

Either bad books, or books describing only the language.
DOM parsers are not part of that, but part of the AOM/DOM.
Where is an authoritative reference (online or otherwise)
where I can find this kind of info?

Which kind of info? said:
Usually you need a host object for that, Mozilla exposes DOMParser e.g.
[...]
Opera 8 also supports DOMParser and has some minimal support for W3C DOM
Level 3 Load and Save too.
<URL:http://www.opera.com/docs/specs/>
I think Safari 2 or at least the latest 2.something release also
supports DOMParser.

<URL:http://www.apple.com/macosx/features/safari/>
With IE/Win for XML you need to use MSXML anyway and there DOMDocument
exposes a method named loadXML to parse a string and create an XML DOM
document.

<URL:http://msdn.microsoft.com/XML/BuildingXML/default.aspx>


Google is your friend. [psf 6.1]


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
474,431
Messages
2,571,679
Members
48,796
Latest member
Greg L.

Latest Threads

Top