4

Problem at hand: I have XML on the client side and the server side, but there's no consistent way to parse XML on both sides.

Most browsers have DOMParser. But Node.js doesn't have a built-in XML parser. There are lots of modules for Node.js for XML parsing, but I'm looking for a XML parser API that is consistent for both front-end and back-end. In other words, I'm looking for a XML parsing module that can be used in Node.js like this

const parser = require(magic_library);
const doc = parser.parseFromString(xml_string, 'application/xml');

and also in the browser like this

<script src="magic_library"></script>
<script>
  const doc = parser.parseFromString(xml_string, 'application/xml');
</script>
2
  • Please see the help center, specifically item #4 in the section with the numbered list. Commented Aug 15, 2017 at 1:17
  • Try fast-xml-parser Commented Aug 15, 2017 at 1:41

1 Answer 1

2

Try fast-xml-parser

In Node.js

var fastXmlParser = require('fast-xml-parser');
var jsonObj = fastXmlParser.parse(xmlData);

// when a tag has attributes 
var options = {
    attrPrefix : "@_",
    textNodeName : "#text",
    ignoreNonTextNodeAttr : true,
    ignoreTextNodeAttr : true,
    ignoreNameSpace : true,
    ignoreRootElement : false,
    textNodeConversion : true,
    textAttrConversion : false,
    arrayMode : false
};
if(fastXmlParser.validate(xmlData)=== true){//optional 
    var jsonObj = fastXmlParser.parse(xmlData,options);
}

//Intermediate obj 
var tObj = fastXmlParser.getTraversalObj(xmlData,options);
var jsonObj = fastXmlParser.convertToJson(tObj);

And in browser

var isValid = parser.validate(xmlData);
var jsonObj = parser.parse(xmlData);
Sign up to request clarification or add additional context in comments.

8 Comments

But note the limitations section: "The parser does not check whether the XML is valid or not". (And I suspect that by "valid", it means "well-formed"). Sadly, finding a high quality conformant XML parser for Javascript is very difficult (I haven't succeeded yet). And I won't believe a parser has a good level of conformance unless it publishes its results against the W3C conformance tests (w3.org/XML/Test/xmlconf-20020606.htm)
it depends on use cases. most performant parser usually sacrificing xml validation for speed.
I've taken a look at the source and this looks like something written by an amateur in an afternoon. It doesn't even handle basic things like character and entity references. The authors don't seem to know the proper meaning of the terms "well-formed" and "valid" as used in the XML world. And it doesn't produce a DOM tree, which is surely implicit in the OP's requirement.
Re-reading that comment: by "amateur" I didn't mean an amateur as far as programming skills are concerned, I meant someone whose knowledge of XML is fairly limited. Sorry if I implied otherwise.
How do I import this into the browser? I'm using Express. Do I just have a tag like. <script src="../node_modules/fast-xml-parser"></script>
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.