1.0.5 • Published 4 years ago
prabhat-xml-parser v1.0.5
prabhat-xml-parser
This is a parser that provides much more beautiful output than anyother javascript parser. I tried xml2js and others but each one of them gave me unweildy json from xml which was very difficult to read.
I also needed the functionality where I want to skip parsing of a specific tag and return it as it is. This parser has this functionality too.
So here we go!
sample usage
var fs = require('fs');
const util = require('util')
var parser = require('prabhat-xml-parser')
var xmldoc = require('xmldoc');
// var filename = __dirname + '/test-bigfile.xml'
var filename = __dirname + '/test-smallfile.xml'
fs.readFile(filename, function (err, data) {
var xml = data.toString('utf8')
var document = new xmldoc.XmlDocument(xml)
var processedObject = parser.parse(document,'text') // do not process 'text' tags
console.log( util.inspect(processedObject, {showHidden: false, depth: null}))
})
<?xml version="1.0" encoding="UTF-8"?><?xml-stylesheet href="http://www.accessdata.fda.gov/spl/stylesheet/spl.xsl" type="text/xsl"?>
<document>
<publisher location="San Francisco">Manning</publisher>
<author>
<firstname aka="Bablu">Prabhat</firstname>
<lastnmame>Sharma</lastnmame>
<text> <p>Something in html</p> </text>
</author>
<books>
<book>
<title>A small book</title>
</book>
<book>
<title>A big book</title>
</book>
</books>
</document>
will result in:
{
"publisher": { "attr": { "location": "San Francisco" }, "text": "Manning" },
"author": {
"firstname": { "attr": { "aka": "Bablu" }, "text": "Prabhat" },
"lastnmame": { "text": "Sharma" },
"text": "<text> <p>Something in html</p> </text>"
},
"books": [
{ "title": { "text": "A small book" } },
{ "title": { "text": "A big book" } }
]
}