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.

JS Date Object to XML results in empty tag

See original GitHub issue

Hi!

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:closed
  • Created 4 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

1reaction
KSDaemoncommented, Jul 28, 2019

Great! Thnx!

0reactions
amitguptagwlcommented, Jul 28, 2019

your concern is valid. I’ve fixed the issue. I’ll be publishing them soon.

Read more comments on GitHub >

github_iconTop 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 >

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