XML parser written in pure javascript for embedded environments [closed]

南楼画角 提交于 2021-02-07 14:19:27

问题


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


回答1:


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



回答2:


Checkout jQuery.parseXML - a basic XML parser.



来源:https://stackoverflow.com/questions/20692837/xml-parser-written-in-pure-javascript-for-embedded-environments

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!