hipchat / x2js

This library provides XML to JSON (JavaScript Objects) and vice versa javascript conversion functions. The library is very small and doesn't require any other additional libraries. Forked from https://code.google.com/p/x2js/
0 stars 2 forks source link

Having a comment inside an element in the XML creates sub-level items in the json #15

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
What steps will reproduce the problem?

1. Have embedded comments inside the XML, eg :

<stuff>
  <things>
    Value
    <!-- a comment from somewhere -->
  </things>
</stuff>

- unfortunately these are coming from a production system I have no control 
over.

What is the expected output? What do you see instead?

I would expect to see :

{
  "stuff": {
    "things": "Value"
  }
}

- instead I get :

{
  "stuff": {
    "things": {
      "__text": "Value"
    }
  }
}

What version of the product are you using? On what operating system and
browser version?

latest xml2js code, and every browser - Chrome / IE / FF / etc

Please provide any additional information below.

Traced the issue to the source where you are checking the 'type' of XML node - 
you need a check on line 73(ish) :

eg.... my fix commented with '//cje'

for(var cidx=0; cidx <nodeChildren.length; cidx++) {
    var child = nodeChildren.item(cidx); // nodeChildren[cidx];
    var childName = getNodeLocalName(child);

    if(child.nodeType!=8) { //cje -  not a COMMENT NODE
        result.__cnt++;
        if(result[childName] == null) {
            result[childName] = parseDOMChildren(child);
            result[childName+"_asArray"] = new Array(1);
            result[childName+"_asArray"][0] = result[childName];
        }
        else {
            if(result[childName] != null) {
                if( !(result[childName] instanceof Array)) {
                    var tmpObj = result[childName];
                    result[childName] = new Array();
                    result[childName][0] = tmpObj;

                    result[childName+"_asArray"] = result[childName];
                }
            }
            var aridx = 0;
            while(result[childName][aridx]!=null) aridx++;
            (result[childName])[aridx] = parseDOMChildren(child);
        }
    } //cje - end
}

Hope that helps !

Thanks
Chris

Original issue reported on code.google.com by catdna1...@googlemail.com on 15 Oct 2013 at 1:12

GoogleCodeExporter commented 9 years ago
Thanks for the report!

Original comment by abdulla....@gmail.com on 15 Oct 2013 at 2:40

GoogleCodeExporter commented 9 years ago
v1.1.3 (Available in mercurial - https://x2js.googlecode.com/hg/xml2json.js).
Thanks!

Original comment by abdulla....@gmail.com on 15 Oct 2013 at 6:11

GoogleCodeExporter commented 9 years ago
v1.1.3 is released

Original comment by abdulla....@gmail.com on 16 Oct 2013 at 7:25