元素树:如何查找具有特定值的所有子元素

时间:2014-03-21 13:35:47

标签: python elementtree

我有一个XML文件,结构如下:

<?xml version="1.0"?>
<data>
<product>
    <Product_Code>code1</Product_Code>
    <Size>x</Size>
    <Quantity>1<Quantity>
</product>
<product>
    <Product_Code>code3</Product_Code>
    <Size>c</Size>
    <Quantity>5<Quantity>
</product>
<product>
    <Product_Code>code2</Product_Code>
    <Size>z</Size>
    <Quantity>2<Quantity>
</product>
<product>
    <Product_Code>code3</Product_Code>
    <Size>a</Size>
    <Quantity>1<Quantity>
</product>
<product>
    <Product_Code>code1</Product_Code>
    <Size>y</Size>
    <Quantirt>1<Quantity>
</product>
<product>
    <Product_Code>code3</Product_Code>
    <Size>b</Size>
    <Quantity>5<Quantity>
</product>
</data>

XML中有产品。每个产品都有代码,大小和数量。代码可能很常见。

我想选择与每个尺码对应的每个代码和数量对应的所有不同尺寸。 即: {code1:{x:1,y:1},code2:{z:2},code3:{a:1,b:5,c:5}

修改: 我想在旅途中做。例如。如果遍历子元素:

tree = ET.parse(file)
root = tree.getroot()
for child in root:
    code = child.find('Product_Code')

如果code =“code1”,我想找到root中的所有其他元素withcode =“code1”及其相应的大小和数量值。

我想生成以下XML:

<products>
    <product>
        <Product_Code>code1<Product_Code>
        <variants>
            <variant>
                <size>x</size>
                <quantity>1</quantity>
            </variant>
            <variant>
                <size>y</size>
                <quantity>1</quantity>
            </variant>
        </variants>
    </product>
    <product>
        <Product_Code>code2<Product_Code>
        <variants>
            <variant>
                <size>z</size>
                <quantity>2</quantity>
            </variant>
        </variants>
    </product>
    <product>
        <Product_Code>code3<Product_Code>
        <variants>
            <variant>
                <size>a</size>
                <quantity>1</quantity>
            </variant>
            <variant>
                <size>b</size>
                <quantity>5</quantity>
            </variant>
            <variant>
                <size>c</size>
                <quantity>5</quantity>
            </variant>
        </variants>
    </product>
</products>

1 个答案:

答案 0 :(得分:2)

首先,您的xml无效,请参阅不匹配的QuantiryQuantity标记。

无论如何,以下是您可以获得的代码,例如,每个代码的所有尺寸:

from xml.etree import ElementTree as ET


data = """<?xml version="1.0"?>
<data>
<product>
    <Product_Code>code1</Product_Code>
    <Size>x</Size>
    <Quantity>1</Quantity>
</product>
<product>
    <Product_Code>code3</Product_Code>
    <Size>c</Size>
    <Quantity>5</Quantity>
</product>
<product>
    <Product_Code>code2</Product_Code>
    <Size>z</Size>
    <Quantity>2</Quantity>
</product>
<product>
    <Product_Code>code3</Product_Code>
    <Size>a</Size>
    <Quantity>1</Quantity>
</product>
<product>
    <Product_Code>code1</Product_Code>
    <Size>y</Size>
    <Quantity>1</Quantity>
</product>
<product>
    <Product_Code>code3</Product_Code>
    <Size>b</Size>
    <Quantity>5</Quantity>
</product>
</data>"""

tree = ET.fromstring(data)

codes = {}
for product in tree.findall('.//product'):
    code = product.find('Product_Code').text
    size = product.find('Size').text
    quantity = product.find('Quantity').text

    codes[code] = codes[code] if code in codes else {}
    codes[code][size] = quantity

print codes

打印:

{'code1': {'y': '1', 'x': '1'}, 'code2': {'z': '2'}, 'code3': {'a': '1', 'c': '5', 'b': '5'}}