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).

Coyote
  • 2,454
  • 26
  • 47
  • 1
    Googling `javascript parse xml` shows lots of useless results (using browser capabilities) but also some useful ones – Pekka Dec 19 '13 at 22:23
  • Useful answer here: http://stackoverflow.com/a/7373003/1388017 – dezman Dec 19 '13 at 22:23
  • 1
    Also http://stackoverflow.com/a/9056881/187606 it doesn't outright *say* it's a complete native implementation but it looks like one – Pekka Dec 19 '13 at 22:24
  • xhr's responseXML is a pure parsed DOM. btw what you mean by no browser parser? – cocco Dec 19 '13 at 22:28
  • @cocco A parser that uses no browser extensions like ActiveXObject or DOMParser. Simply put by providing a pure javascript implementation. – Coyote Dec 19 '13 at 22:37
  • @Pekka웃 [marknote](https://code.google.com/p/marknote/wiki/DevelopersGuide) is indeed a pure js implementation. I'll use that. Thank you! – Coyote Dec 19 '13 at 22:42
  • if new XMLHttpRequest is aviable you prolly can use just the responseXML. – cocco Dec 19 '13 at 22:45
  • @cocco I need to load the XML from memory and eventually from files too but the files are uncompressed by C++ and transferred to javascript as strings. So I need to read the strings. – Coyote Dec 19 '13 at 22:53
  • i added an example marknote uses ajax. yeah reading strings is not possible.. but if that works reading files it's for sure faster than using the makernote parser. – cocco Dec 19 '13 at 22:53
  • @cocco : parseURL() uses ajax but parse() runs its own parser. It parses strings. – Coyote Dec 19 '13 at 23:01
  • i understand... wasn't 100% sure of your question and what the c++ can handle. – cocco Dec 19 '13 at 23:04
  • I don't see why this question is off topic. The requirement is very specific and there are no solutions specifically created for this context. If this question is off-topic all other questions about libraries for nodejs, spider monkey, python, php etc are off-topic. i.e.: http://stackoverflow.com/q/14890655/171711 – Coyote Dec 22 '13 at 15:56
  • @Quentin I think the question Makes more sense now. It was badly worded and lacked the context in its previous form, could be reopened now I think. – Coyote Dec 27 '13 at 14:22

2 Answers2

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
Community
  • 1
  • 1
Coyote
  • 2,454
  • 26
  • 47
-5

Checkout jQuery.parseXML - a basic XML parser.

Vladimir Georgiev
  • 1,949
  • 23
  • 26
  • 3
    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). – Coyote Dec 19 '13 at 22:35