Reputation: 329
I have an xml like this but i want to convert the xml into proper xml by node.js can anyone help me in this
<?xml version = 1.0 encoding = utf-8?>
<!-- xslplane.1.xml -->
<?xml-stylesheet type = text/xsl href = xslplane.1.xsl ?>
<plane>
<year> 1977 </year>
<make> Cessna </make>
<model> Skyhawk </model>
<color> Light blue and white </color>
</plane>
I want XML like this with all the possible quotations
<?xml version = "1.0" encoding = "utf-8"?>
<!-- xslplane.1.xml -->
<?xml-stylesheet type = "text/xsl" href = "xslplane.1.xsl" ?>
<plane>
<year> 1977 </year>
<make> Cessna </make>
<model> Skyhawk </model>
<color> Light blue and white </color>
</plane>
Upvotes: 0
Views: 640
Reputation: 3266
You can try using xml2js to convert your malformed xml to JSON and use it again to convert to xml. Try the following code.
let xml2js = require('xml2js');
var xml = "<hi>Hello xml2js!</hi>";
xml2js.parseString(xml, function (err, result) {
console.dir(result);
var builder = new xml2js.Builder();
var xml = builder.buildObject(result);
console.log(xml)
});
Update : Direct File read
var parser = new xml2js.Parser();
fs.readFile(__dirname + '/foo.xml', function(err, data) {
parser.parseString(data, function (err, result) {
console.dir(result);
console.log('Done');
var builder = new xml2js.Builder();
var xml = builder.buildObject(result);
console.log(xml)
});
});
Upvotes: 1