JS Date Object to XML results in empty tag
See original GitHub issueHi!
Parsing Date object to XML results in empty tag.
Checklist
- Have you asked your question on Stackoverflow or similar forum?
- Are you running the latest version?
- Have you included sample input, output, error, and expected output?
- Have you checked if you are using correct configuration?
- Did you try online tool?
Input && Code
const j2xParser = require('fast-xml-parser').j2xParser;
let data, parser;
parser = new j2xParser({ format: true });
data = parser.parse({ root: { data: new Date() } });
console.log(data);
Output
<root>
<data>
</data>
</root>
expected data
<root>
<data>Tue Jul 16 2019 11:01:29 GMT+0300 (Москва, стандартное время)</data>
</root>
I’m in RU locale. So it looks like this. What I mean — expected behavior is to see the output of date.toString()
Would you like to work on this issue?
- Yes
- No
- Maybe 😃
As I looked through the sources. A problem is here: https://github.com/NaturalIntelligence/fast-xml-parser/blob/master/src/json2xml.js#L96
typeof Date object is object
Can be fixed like this:
- } else if (typeof jObj[key] !== 'object') {
+ } else if (typeof jObj[key] !== 'object' || jObj[key] instanceof Date) {
I don’t know, is it enough or not. It’s working, but maybe there are some things I don’t know about.
Also, it will be great to have an option to control how tag values are processed. There is tagValueProcessor
option, but it takes as input string, not an original value, as I see here:
function buildTextValNode(val, key, attrStr, level) {
return (
this.indentate(level) +
'<' +
key +
attrStr +
'>' +
this.options.tagValueProcessor('' + val) + //HERE
'</' +
key +
this.tagEndChar
);
}
Issue Analytics
- State:
- Created 4 years ago
- Comments:5 (2 by maintainers)
Top Results From Across the Web
Fill an empty XML tag using JavaScript - Stack Overflow
First of all, there's a typo in your XML. Change <shipping_number notFilterable="true"></shipping_number.
Read more >Writing XML Output from Objects - InterSystems Documentation
This topic describes to write XML output from InterSystems IRIS objects, ... that contains only whitespace characters is converted to an empty element....
Read more >XML Elements - W3Schools
Empty XML Elements ... An element with no content is said to be empty. ... The two forms produce identical results in XML...
Read more >Built-ins for strings - Apache FreeMarker Manual
You can also specify the format explicitly like ?datetime.format (and hence also as ... it removes the empty elements from the end of...
Read more >XML | Elements - GeeksforGeeks
Empty Elements : An element in XML document which does not contains the content is known ... Output: G4G Geeksforgeeks 2345456767. Example 2:....
Read more >
Top Related Medium Post
No results found
Top Related StackOverflow Question
No results found
Troubleshoot Live Code
Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free
Top Related Reddit Thread
No results found
Top Related Hackernoon Post
No results found
Top Related Tweet
No results found
Top Related Dev.to Post
No results found
Top Related Hashnode Post
No results found
Great! Thnx!
your concern is valid. I’ve fixed the issue. I’ll be publishing them soon.