xml-js
xml-js copied to clipboard
Failed to convert back into a xml file
I need to convert xml to json and then make some changes to json and convert it back to xml. This module seems very promising.
When I tried it on some xml (https://raw.githubusercontent.com/GovReady/govready/master/prototypes/ssg-rhel6-oval.xml)
The converted xml is not valid: I loaded it using Firefox browser, but it complained XML Parsing Error: not well-formed
.
Here is a simple code snippet that I adopted from your example.
var convert = require('xml-js');
var fs=require("fs");
xml=fs.readFileSync("ssg-rhel6-oval.xml", "utf8");
var result1 = convert.xml2json(xml, {compact: true, spaces: 4});
rxml = convert.json2xml(result1, {compact: true});
console.log(rxml);
Any ideas? Appreciate your help!
@jinq102030 did you solve this?
I didn't need it any more. You can close this ticket.
'<' and '&' need to be represented as character /numeric entities in attribute values (See XML 1.0 Standard section 2.4, https://www.w3.org/TR/xml/#syntax). This does not happen and causes the trouble with the sample file of jinq102030.
Minimal test case (see attached files: test.zip):
- orig
<root>
<test sample="<>&'""/>
</root>
- written after parse
<root>
<test sample="<>&'""/>
</root>
- expected
<root>
<test sample="<>&'""/>
</root>