javascript - XML Parser that works for both browser and Node.js? -


problem @ hand: have xml on client side , server side, there's no consistent way parse xml on both sides.

most browsers have domparser. node.js doesn't have built-in xml parser. there lots of modules node.js xml parsing, i'm looking xml parser api consistent both front-end , back-end. in other words, i'm looking xml parsing module can used in node.js this

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

and in browser this

<script src="magic_library"></script> <script>   const doc = parser.parsefromstring(xml_string, 'application/xml'); </script> 

try fast-xml-parser

in node.js

var fastxmlparser = require('fast-xml-parser'); var jsonobj = fastxmlparser.parse(xmldata);  // when 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); 

Comments

Popular posts from this blog

android - InAppBilling registering BroadcastReceiver in AndroidManifest -

python Tkinter Capturing keyboard events save as one single string -

sql server - Why does Linq-to-SQL add unnecessary COUNT()? -