Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

j2xParser.parse throws error if value is null #86

Closed
sigoden opened this issue May 8, 2018 · 2 comments
Closed

j2xParser.parse throws error if value is null #86

sigoden opened this issue May 8, 2018 · 2 comments
Labels

Comments

@sigoden
Copy link

sigoden commented May 8, 2018

var Parser = require("fast-xml-parser").j2xParser;
new Parser().parse({ value: null })

error:

TypeError: Cannot convert undefined or null to object
    at Function.keys (<anonymous>)
    at Parser.j2x (/tmp/xml2/node_modules/fast-xml-parser/src/j2x.js:79:25)
    at Parser.j2x (/tmp/xml2/node_modules/fast-xml-parser/src/j2x.js:137:37)
    at Parser.parse (/tmp/xml2/node_modules/fast-xml-parser/src/j2x.js:73:17)

expect: <value />

@csantos1113
Copy link

@amitguptagwl - question
I have this XML:

<svg xmlns="http://www.w3.org/2000/svg" width="100" height="36" viewBox="0 0 100 36">
  <title>Some</title>
  <path d="M15.06,5.6a15.07,15.07,0,0,0,0,30.13Z" fill="red"/>
  <path d="M15.06,5.6a15.07,50.07,0,0,0,0,30.13Z" fill="blue"/>
</svg>

And I'm just transforming to object, and then transforming back to XML:

const parserOptions = {
  format: false,
  ignoreAttributes: false
};
const parser = require('fast-xml-parser');
const traversalObj = parser.getTraversalObj(xml, parserOptions);
const svgData = parser.convertToJson(traversalObj, parserOptions);

const Parser = new parser.j2xParser(parserOptions);
console.log(Parser.parse(svgData));

I'm getting

<svg xmlns="http://www.w3.org/2000/svg" width="100" height="36" viewBox="0 0 100 36">
  <title>Some</title>
  <path d="M15.06,5.6a15.07,15.07,0,0,0,0,30.13Z" fill="red"></path>
  <path d="M15.06,5.6a15.07,50.07,0,0,0,0,30.13Z" fill="blue"></path>
</svg>

I was expecting to receive self-closed tags for the <path />s.

How can I do it?

Thanks!

@amitguptagwl
Copy link
Member

@csantos1113 if you check README, you 'll find following property to use at the time of parsing back;

supressEmptyNode : If set to true, tags with no value (text or nested tags) are written as self closing tags.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

No branches or pull requests

3 participants