Element Tree: How to find all child elements with a specific value

nish

I have an XML file with following structure:

<?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>

There are products in the XML. Each product has a code,size and quantoty. Code can be common.

I want to select all different size corresponding to each code and quantity corresponding to each size. i.e: {code1: {x:1, y:1}, code2: {z:2}, code3: {a:1,b:5,c:5}

EDIT: I want to do it on the go. E.g. If iterate through the child elements as:

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

If code = "code1", I'd like to find all other elements in the root withcode = "code1" and their corresponding size and quantity value.

I want to generate following 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>
alecxe

First of all, your xml is not valid, see mismatched Quantiry and Quantity tags.

Anyway, here's how you can get, for example, all of the sizes per code:

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

prints:

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

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

how to find specific class through all child elements and if available then hide that child element?

From Dev

How to find the number of elements in element tree in python?

From Dev

How to find the number of elements in element tree in python?

From Dev

Parsing evernote xml with element tree on python: how to find/delete elements with specific tag name

From Dev

Use XSLT/XPATH to select elements having a child element with a specific value

From Dev

Use XSLT/XPATH to select elements having a child element with a specific value

From Dev

How to find a child element with a specific value and delete it's immediate parent node?

From Dev

How to find a child element with a specific value and delete it's immediate parent node?

From Dev

in C++, how can I find all elements of an array linked directly or indirectly to a specific element of the array?

From Dev

XPATH - Find element where child items has a specific value

From Dev

Efficient way to find how many neighbouring elements with a specific value there are around a particular element in an array

From Dev

Efficient way to find how many neighbouring elements with a specific value there are around a particular element in an array

From Dev

How to find and check all dynamic child checkboxes in tree using jquery?

From Dev

Select all the elements within an element having an attribute set to a specific value

From Dev

How to get all the child elements from a specific div

From Dev

How to get all Child Elements with specific attribute in JavaScript

From Dev

How to get all child elements in the parent element of xml in c#

From Dev

Find documents where ALL elements of an array have a specific value

From Dev

Find documents where ALL elements of an array have a specific value

From Dev

XSLT: Display all elements containing a specific child element (and only those elements)

From Dev

one listener for all child elements in the DOM tree

From Dev

How to get value from a specific child element in XML using XmlReader?

From Dev

How to get all the elements in the list without an element at a specific index in python?

From Dev

How to find the parent element If I don't know number of elements that is present between the child element and the Parent element

From Dev

How to find out, if all specific Keys got a specific Value in an multiarray?

From Dev

Find elements which have a specific child with BeautifulSoup

From Dev

Python - Beautifulsoup, check for a child element with specific condition inside a find_all() result

From Dev

Get List of Elements in Tree with specific Field Value

From Dev

How to find HTML element with a data attribute which contains a specific value?

Related Related

  1. 1

    how to find specific class through all child elements and if available then hide that child element?

  2. 2

    How to find the number of elements in element tree in python?

  3. 3

    How to find the number of elements in element tree in python?

  4. 4

    Parsing evernote xml with element tree on python: how to find/delete elements with specific tag name

  5. 5

    Use XSLT/XPATH to select elements having a child element with a specific value

  6. 6

    Use XSLT/XPATH to select elements having a child element with a specific value

  7. 7

    How to find a child element with a specific value and delete it's immediate parent node?

  8. 8

    How to find a child element with a specific value and delete it's immediate parent node?

  9. 9

    in C++, how can I find all elements of an array linked directly or indirectly to a specific element of the array?

  10. 10

    XPATH - Find element where child items has a specific value

  11. 11

    Efficient way to find how many neighbouring elements with a specific value there are around a particular element in an array

  12. 12

    Efficient way to find how many neighbouring elements with a specific value there are around a particular element in an array

  13. 13

    How to find and check all dynamic child checkboxes in tree using jquery?

  14. 14

    Select all the elements within an element having an attribute set to a specific value

  15. 15

    How to get all the child elements from a specific div

  16. 16

    How to get all Child Elements with specific attribute in JavaScript

  17. 17

    How to get all child elements in the parent element of xml in c#

  18. 18

    Find documents where ALL elements of an array have a specific value

  19. 19

    Find documents where ALL elements of an array have a specific value

  20. 20

    XSLT: Display all elements containing a specific child element (and only those elements)

  21. 21

    one listener for all child elements in the DOM tree

  22. 22

    How to get value from a specific child element in XML using XmlReader?

  23. 23

    How to get all the elements in the list without an element at a specific index in python?

  24. 24

    How to find the parent element If I don't know number of elements that is present between the child element and the Parent element

  25. 25

    How to find out, if all specific Keys got a specific Value in an multiarray?

  26. 26

    Find elements which have a specific child with BeautifulSoup

  27. 27

    Python - Beautifulsoup, check for a child element with specific condition inside a find_all() result

  28. 28

    Get List of Elements in Tree with specific Field Value

  29. 29

    How to find HTML element with a data attribute which contains a specific value?

HotTag

Archive