buglabs/node-xml2json

xml2json is losing textnodes

denghauser opened this issue · 2 comments

When I have this XML
<foo attr=\"value\">bar<subnode val=\"test\" >glass</subnode></foo>
I would expect this JSON

{"foo":{"attr":"value","$t":"bar","subnode":{"val":"test","$t":"glass"}}} but I get:
{"foo":{"attr":"value","subnode":{"val":"test","$t":"glass"}}}

Losing the "$t":"bar"

I found in the code of xml2json.js -> endElement(name) ... that you delete the textNodeName element of the currentObject. Why would you do this? This removes all text elements but the one on the deepest child node.

Is this by design?

Also encountering this issue. It looks like it was added here: 8538475

to fix some spaces disappearing. It does not seem to be by design but a side effect.

@denghauser just curious, how did you address this on your end?

<foo attr="value">bar<subnode val="test" >glass</subnode></foo>

may be converted to:

{
  "foo": {
    "-attr": "value",
    "#text": "bar",
    "subnode": {
      "-val": "test",
      "#text": "glass"
    }
  },
  "#omit-xml-declaration": "yes"
}