Facing Error in parsing xml file using node js

I have been trying to parse the XML file using ‘xml-js’. I was getting an error of invalid XML data. I tried formatting the XML content online, after formatting I was able to parse. So I am trying to format the contents first using ‘xml-formatter’. But I am getting the following error. Kindly help me to solve the above problem. Thank you.

    var format = require('xml-formatter');     var xml = await fs.readFile("Master.xml", "utf8");     var formattedXml = format(xml); 

Error

Error: Failed to parse XML at document (D:\project\node_modules\xml-parser-xo\index.js:49:19) at parse (D:\project\node_modules\xml-parser-xo\index.js:236:12) at format (D:\project\node_modules\xml-formatter\index.js:141:23) at D:\projectImportRouter.js:97:28 
Add Comment
1 Answer(s)

Without seeing the XML (and without a more specific error message) it’s impossible to say.

xml-js claims to be "fully XML compliant"; but if you look at its dependencies, it depends on a SAX library that acknowledges restrictions, for example in the area of DTD and entity processing. My guess would be that your XML comes in the area covered by those restrictions, and that your online formatter removed the offending constructs.

The xml-formatter package in turn depends on the xml-parser package which says upfront that it is a "Simple non-compiant XML parser" [sic], without saying what the restrictions are, so relying on it to clean up your XML is optimistic at best.

I’m afraid the standard of compliance of XML parsers in the Javascript world leaves a great deal to be desired. We took a good look around when developing Saxon-JS, and all the products we looked at were deficient.

Add Comment

Your Answer

By posting your answer, you agree to the privacy policy and terms of service.