5

I'm looking for a parser which can run in a javascript environment where there is no access to document, DOMParser or any other browser extension. The javascript application can run in browsers (IE, Firefox, Chrome, Safari...) in node.js but it is destined to run mainly embedded in a V8 or in a SpiderMonkey environment. The environment is distributed without support for the usual XML parsers and I am unable to parse a string containing valid XML from javascript.

All libraries which rely on browser extensions like DOMParser and ActiveXObject fail with messages like ReferenceError: DOMParser is not defined.

Access to file system is not necessary (I need to parse from string to a DOM-like structure).

13
  • 1
    Googling javascript parse xml shows lots of useless results (using browser capabilities) but also some useful ones Commented Dec 19, 2013 at 22:23
  • Useful answer here: stackoverflow.com/a/7373003/1388017 Commented Dec 19, 2013 at 22:23
  • 1
    Also stackoverflow.com/a/9056881/187606 it doesn't outright say it's a complete native implementation but it looks like one Commented Dec 19, 2013 at 22:24
  • xhr's responseXML is a pure parsed DOM. btw what you mean by no browser parser? Commented Dec 19, 2013 at 22:28
  • @cocco A parser that uses no browser extensions like ActiveXObject or DOMParser. Simply put by providing a pure javascript implementation. Commented Dec 19, 2013 at 22:37

2 Answers 2

4

marknote is the right solution as noted here (thanks to Pekka 웃). The library uses XMLHttpRequest when loading from remote locations, but when parsing from a string it integrates a standalone XML parser written in javascript, which makes it suitable for use in embedded interpreters :

var text="<note>";
text=text+"<content>whatever blablabla</content>";
text=text+"</note>";


var parser = new marknote.Parser();
var doc = parser.parse(text);

native.log(doc.toString()); // show the formatted XML
Sign up to request clarification or add additional context in comments.

1 Comment

There are many pure js solutions are available now.
-5

Checkout jQuery.parseXML - a basic XML parser.

1 Comment

Actually this is exactly what the OP excludes: as per JQuery source: new DOMParser(); this is a browser extension. Jquery simply hides the parser implementation (ActiveXObject and DOMParser).

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.