3

我目前有一个以下格式的 xml 文件:

<?xml version="1.0" encoding="UTF-8" ?>
    <Garden>
        <id>97</id>
        <Flowers>
            <id>98</id>
            <Type>
                <id>99</id>
                <Level>
                    <id>100</id>                    
                </Level>
            </Type>
        </Flowers>
    </Garden>

我想用xmltodict这个 xml 转换成字典,这很简单。但是我想做一个轻微的修改。

我想让我的 json 变成这样的东西。

{
    "Garden": {
        "id": "97",
        "state": "0",
        "Flowers": {
            "id": "98",
            "state": "0",
            "Type": {
                "id": "99",
                "state": "0",
                "Level": {
                    "id": "100",
                    "state": "0"                

                }
            }
        }
    }
}

我希望能够"state": "0"为所有级别添加默认值。我真的很困惑如何做到这一点。任何帮助将非常感激。

这是我现在所拥有的:

with open("gardenlist.xml", 'r') as file:
    xmlString = file.read() 
print(xmlString)     
jsonString = json.dumps(xmltodict.parse(xmlString), indent=4)

这只是打印 json 但没有"state": "0"值。

4

3 回答 3

3

Imo 这是拥有自己的解析器的好机会:

from parsimonious.grammar import Grammar
from parsimonious.nodes import NodeVisitor, RegexNode

xml = """
<?xml version="1.0" encoding="UTF-8" ?>
    <Garden>
        <id>97</id>
        <Flowers>
            <id>98</id>
            <Type>
                <id>99</id>
                <Level>
                    <id>100</id>                    
                </Level>
            </Type>
        </Flowers>
    </Garden>
"""

class XMLVisitor(NodeVisitor):
    grammar = Grammar(
        r"""
        program     = root expr+
        expr        = opentag list closetag
        item        = (opentag notpar closetag) / expr
        list        = item+

        root        = ws? lpar "?xml" notpar rpar
        opentag     = ws? lpar word rpar ws?
        closetag    = lpar slash word rpar ws?

        lpar        = "<"
        rpar        = ">"
        notpar      = ~"[^<>]+"
        slash       = "/"

        word        = ~"\w+"
        ws          = ~"\s+"
        """
    )

    def generic_visit(self, node, visited_children):
        return visited_children or node

    def visit_opentag(self, node, visited_children):
        ws, lpar, word, *_ = visited_children
        return word.text

    def visit_closetag(self, node, visited_children):
        lpar, slash, word, *_ = visited_children
        return word.text

    def visit_notpar(self, node, visited_children):
        return node.text

    def visit_item(self, node, visited_children):
        if len(visited_children[0]) == 3:
            # first branch
            opentag, content, *_= visited_children[0]
            return (opentag, content)
        else:
            return visited_children[0]

    def visit_expr(self, node, visited_children):
        tag, lst, *_ = visited_children
        return (tag, lst)

    def visit_program(self, node, visited_children):
        root, content = visited_children
        return self.__makeDict__(content)

    def __makeDict__(self, struct, level = 0):
        d = {}
        for item in struct:
            key, value = item
            if isinstance(value, list):
                value = self.__makeDict__(value)
            d[key] = value
            d["state"] = 0
        return d

visitor = XMLVisitor()
output = visitor.parse(xml)

print(output)

这个易于理解的片段正确产生

{'Garden': {'id': '97', 'state': 0, 'Flowers': {'id': '98', 'state': 0, 'Type': {'id': '99', 'state': 0, 'Level': {'id': '100', 'state': 0}}}}, 'state': 0}
于 2019-07-24T18:00:46.443 回答
2

我想说正确的方法是准备所需的XML结构 - 然后将其转换为dictjson字符串:

复杂的方法:

import xml.etree.ElementTree as ET
import xmltodict
import json

tree = ET.parse('gardenlist.xml')
root = tree.getroot()

state_el = ET.Element('state')    # prepare `state` node
state_el.text = '0'
root.insert(1, state_el)

def add_state(root, el_to_insert):
    for el in root:
        if len(list(el)):    # check if element has child nodes
            el.insert(1, el_to_insert)
            add_state(el, el_to_insert)


add_state(root, state_el)
json_str = json.dumps(xmltodict.parse(ET.tostring(root, encoding="unicode")), indent=4)
print(json_str)

实际输出:

{
    "Garden": {
        "id": "97",
        "state": "0",
        "Flowers": {
            "id": "98",
            "state": "0",
            "Type": {
                "id": "99",
                "state": "0",
                "Level": {
                    "id": "100",
                    "state": "0"
                }
            }
        }
    }
}
于 2019-07-22T20:00:49.240 回答
1

您可以在获取字典后递归地执行此操作。您可以检查哪些值还包含字典并将目标添加到这些值中。看:

import collections 

def addAttr(target):
    target["state"] = "0"
    for key in target:
        if isinstance(target[key], collections.OrderedDict):
            addAttr(target[key])

d1 = xmltodict.parse(xmlString)
addAttr(d1["Garden"])
于 2019-07-22T19:24:19.173 回答