sanand0 / xmljson

xmlsjon converts XML into Python dictionary structures (trees, like in JSON) and vice-versa.
MIT License
122 stars 33 forks source link

Abdera child nodes incorrectly placed in same dictionaries #21

Closed mattgd closed 7 years ago

mattgd commented 7 years ago

I seemed to have encountered a bug with the Abdera conversion logic, specifically how it converts children:

for child in children:
            child_data = self.data(child)
            if (count[child.tag] == 1
                    and len(children_list) > 1
                    and isinstance(children_list[-1], dict)):
                # Merge keys to existing dictionary
                children_list[-1].update(child_data)
            else:
                # Add additional text
                children_list.append(self.data(child))

When given the following XML data:

<?xml version="1.0" encoding="ISO-8859-1"?>
<Data
   version="9.0"
   xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
   xsi:noNamespaceSchemaLocation="comp.xsd">
    <Airport
      country="Samoa"
      city="Apia"
      name="Faleolo Intl"
      lat="-13.8296668231487"
      lon="-171.997166723013"
      alt="17.678M"
      ident="NSFA"
      >
      <Services>
         <Fuel
            type="JETA"
            availability="YES"/>
      </Services>
      <Tower
         lat="-13.8320958986878"
         lon="-171.998676359653"
         alt="0.0M">
      </Tower>
      <Runway
         lat="-13.8300792127848"
         lon="-172.008545994759"
         alt="17.678M"
         surface="ASPHALT"
         heading="89.3199996948242"
         length="2999.23M"
         width="45.11M"
         number="08"
         designator="NONE">
      </Runway>
    </Airport>
</Data>

I receive the following output from ab.data(xml_file):

{
    "Data": {
        "attributes": {
            "version": 9.0,
            "{http://www.w3.org/2001/XMLSchema-instance}noNamespaceSchemaLocation": "comp.xsd"
        },
        "children": [
            {
                "Airport": {
                    "attributes": {
                        "country": "Samoa",
                        "city": "Apia",
                        "name": "Faleolo Intl",
                        "lat": -13.8296668231487,
                        "lon": -171.997166723013,
                        "alt": "17.678M",
                        "ident": "NSFA"
                    },
                    "children": [
                        {
                            "Services": {
                                "Fuel": {
                                    "attributes": {
                                        "type": "JETA",
                                        "availability": "YES"
                                    }
                                }
                            }
                        },
                        {
                            "Tower": {
                                "attributes": {
                                    "lat": -13.8320958986878,
                                    "lon": -171.998676359653,
                                    "alt": "0.0M"
                                }
                            },
                            "Runway": {
                                "attributes": {
                                    "lat": -13.8300792127848,
                                    "lon": -172.008545994759,
                                    "alt": "17.678M",
                                    "surface": "ASPHALT",
                                    "heading": 89.3199996948242,
                                    "length": "2999.23M",
                                    "width": "45.11M",
                                    "number": 8,
                                    "designator": "NONE"
                                }
                            }
                        }
                    ]
                }
            }
        ]
    }
}

The first child node, Services, is placed into a separate dictionary, while the rest of the children are placed in the same dictionary. Because multiple tags with the same keys can be present in XML, all children should probably be placed in separate dictionaries.

I'm working on a PR for this, but if you happen to no what the problem is, let me know.

dagwieers commented 7 years ago

I guess this one should be closed now ?