简体   繁体   中英

Increment the value of a node in the json after converting the xml to json structure

I have an xml of the current format. I convert this xml to a json using the xmltodict library in python.

<?xml version="1.0" encoding="UTF-8" ?>
<MyHouse>   
    <Garden>            
        <InfoList>
            <status value = "0"/>                   
        </InfoList>
            <Flowers>
                <InfoList>
                    <status value = "0"/>   
                </InfoList>             
            </Flowers>          
    </Garden>
</MyHouse>

I want my json dict to look something like this after I send it to the xmltodict method.

json_tree = 
{
  "MyHouse": {
    "Tid": "1",   --> Need to add this node and its value increments from '1'.
    "status": "0",  --> This node is added to the root level node ONLY as it 
                         is not in the xml shown above !!
    "Garden": {
      "Tid": "2", --> Incremeneted to 2
      "InfoList": {
        "status": {
          "@value": "0"
        }
      },
      "Flowers": {
        "Tid": "3", ---> Incremented to 3
        "InfoList": {
          "status": {
            "@value": "0"
          }
        }
      }
    }
  }
}

As we can see in the json structure above, I want to be able to add a default "status": "0" to the root node which is "MyHouse" in this case.

I also want to be able to add the "Tid" for each of the nodes such as "Garden", 'Flowers". Note, there could be many more levels in the xml but it is not shown here for simplicity. I would like to have a generic method.

My current implementation is as the follows.

def add_status(root, el_to_insert):
    # Add "id":"#" to the nodes of the xml
    for el in root:
        if len(list(el)):  # check if element has child nodes
            el.insert(1, el_to_insert)
            el_to_insert = el_to_insert.text + 1 ---> This line of code doesn't seem to work. I want to increment the value of "Tid" everytime its added to the tree? 
            add_status(el, el_to_insert)


def ConverxmltoJson(target):

    xmlConfigFile = ET.parse(target)
    root = xmlConfigFile.getroot()

    state_el = ET.Element("Tid")  # Create `Tid` node, not sure how to add the "status" node to the root "Garden" node.
    state_el.text = "0"
    root.insert(1, state_el)
    add_status(root, state_el)
    json_str = xmltodict.parse(ET.tostring(root, encoding="utf8"))


 with open("xmlconfig.xml") as xmlConfigFile:
        ConverxmltoJson(xmlConfigFile) 

I would be glad if someone could help me to resolve the issue.

Thank you.

I was able to resolve the part in which I had to the "status" to the root node and "Tid" by the following changes.

    state_el = ET.Element("state")  # Create `state` node for root node
    state_el.text = "0"
    root.insert(1, state_el)

    # Adding the Tid node to root level
    id_node = ET.Element("Tid")  # Create `Tid` node
    id_node.text = "0"
    root.insert(1, id_node)

I have a new issue now and opened a new question at link: Updating the "Tid" node value updates to the final value of the global variable

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM