xml-js icon indicating copy to clipboard operation
xml-js copied to clipboard

Failed to convert back into a xml file

Open jinq102030 opened this issue 5 years ago • 3 comments

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 avatar Jan 30 '19 17:01 jinq102030

@jinq102030 did you solve this?

hbt avatar Feb 04 '20 11:02 hbt

I didn't need it any more. You can close this ticket.

jinq102030 avatar Feb 04 '20 16:02 jinq102030

'<' 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="&lt;&gt;&amp;&apos;&quot;"/>
</root>

  • written after parse
<root>
     <test sample="<>&'&quot;"/>
</root>
  • expected
<root>
     <test sample="&lt;>&amp;'&quot;"/>
</root>

SirCodewright avatar Sep 15 '20 11:09 SirCodewright