question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

Exception converting json back to xml

See original GitHub issue

I need to use this module for a project to do some processing on XML --> JSON --> XML.

Checked out the code today and apply it on the xml from here: https://raw.githubusercontent.com/GovReady/govready/master/prototypes/ssg-rhel6-oval.xml

Got error

/Users/jdole/learn/node/node_modules/fast-xml-parser/src/xmlstr2xmlnode.js:48
    xmlData = xmlData.replace(/<!--[\s\S]*?-->/g, "");//Remove  comments
                      ^

TypeError: xmlData.replace is not a function
    at Object.getTraversalObj (/Users/jdole/learn/node/node_modules/fast-xml-parser/src/xmlstr2xmlnode.js:48:23)
    at Object.exports.parse (/Users/jdole/learn/node/node_modules/fast-xml-parser/src/parser.js:10:50)
    at Object.<anonymous> (/Users/jdole/learn/node/xml2js.js:26:20)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:191:16)

The code snippet is adopted from your example.

var parser = require('fast-xml-parser');
var he = require('he');
var fs=require("fs");

var options = {
    attributeNamePrefix : "@_",
    attrNodeName: "attr", //default is 'false'
    textNodeName : "#text",
    ignoreAttributes : false,
    ignoreNameSpace : false,
    allowBooleanAttributes : false,
    parseNodeValue : true,
    parseAttributeValue : false,
    trimValues: true,
    cdataTagName: "__cdata", //default is 'false'
    cdataPositionChar: "\\c",
    localeRange: "", //To support non english character in tag/attribute values.
    parseTrueNumberOnly: false,
    attrValueProcessor: a => he.decode(a, {isAttributeValue: true}),//default is a=>a
    tagValueProcessor : a => he.decode(a) //default is a=>a
};
xml=fs.readFileSync("ssg-rhel6-oval.xml", "utf8");

var result1;
var jsonObj = parser.parse(xml,options);
console.log(parser.parse(jsonObj))

Is this a bug in this module?

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:12 (10 by maintainers)

github_iconTop GitHub Comments

4reactions
Treverixcommented, Jun 19, 2019

The problem he faced was, like mine, that he didn’t convert the file contents to a string.

This causes the error:

xml=fs.readFileSync("ssg-rhel6-oval.xml", "utf8");

And this works:

xml=fs.readFileSync("ssg-rhel6-oval.xml", "utf8").toString();
1reaction
amitguptagwlcommented, Jan 31, 2019

I’ve just tested your file. everything looks fine to me.

const fs = require("fs");
const path = require("path");
const fileNamePath = path.join(__dirname, "./ssg-rhel6-oval.xml");

const xmlData = fs.readFileSync(fileNamePath).toString();

const jsObj = parser.parse(xmlData);
const j2xParser = new parser.j2xParser();
const result = j2xParser.parse(jsObj);
Read more comments on GitHub >

github_iconTop Results From Across the Web

How to convert JSON to XML or XML to JSON? - Stack Overflow
But if you do it, first convert from json to object, then from object to xml (and vice versa for reverse direction). Doing...
Read more >
Converting between JSON and XML - Json.NET
The JsonConvert has two helper methods for converting between JSON and XML. The first is SerializeXmlNode(). This method takes an XmlNode and serializes...
Read more >
Convert a JSON object to XML format in Java? - Tutorialspoint
We can convert a JSONObject into an XML format using org.json.XML class, this provides static methods to convert an XML text into a...
Read more >
Question about "Failed to convert JSON file to XML" Error ...
Error: Failed to convert JSON file to XML. org.jitterbit.integration.server.engine.EngineSessionException: com.fasterxml.jackson.core.
Read more >
How to convert Json to Xml & Xml to Json In Java with Maven
How to convert Json to Xml & Xml to Json In Java with Maven In this tutorial we will learn how to convert...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found