XML ElementTree Python: Find all the relations of a node

Jon Ander Díez

If we supose the following XML file:

<XML Data>
    <Record>
        <Service>
            <Product id="A"></Product>
            <Product id="B"></Product>
            <Product id="C"></Product>
        </Service>
    </Record>
    <Record>
        <Service>
            <Product id="A"></Product>
            <Product id="B"></Product>
            <Product id="Y"></Product>
        </Service>
    </Record>
    <Record>
        <Service>
            <Product id="U"></Product>
        </Service>
    </Record>
</XML Data>

As you can see, each record shows a single client but without an unique identificator. Each service has multiple products.

I want to get all products that have been sold with product A. Therefore, I am trying to get a list like this:

ServiceID
B
C
Y

I've been using:

import xml.etree.ElementTree as ET
mnikley

You can select elements based on an attribute via [@attrib='value'] according to the official documentation. When testing this i exchanged your tag <XML Data> and </XML Data> with <Data> and </Data>. Example code:

from xml.etree import ElementTree as ET

data = ET.parse(r"/path/to/your/input.xml")
root = data.getroot()
for product in root.findall("./Record/Service/Product[@id='A']"):
    print(product.attrib["id"])
    print(product.text)

Edit

After reading your question again i noticed that you first want to check whether a product with id A exists within a Service, and only then store the IDs (uniquely & sorted), so i adapted the code:

from xml.etree import ElementTree as ET

data = ET.parse(r"/path/to/your/input.xml")
root = data.getroot()
product_ids = set()
for service in root.findall("./Record/Service"):
    list_contains_a = False

    # iterate once to identify if list contains product with ID = 'A'
    for product in service.findall("./Product"):
        if product.attrib["id"] == "A":
            list_contains_a = True

    # if list contains product with ID = 'A', iterate second time and fetch IDs
    if list_contains_a:
        for product in service.findall("./Product"):
            if product.attrib["id"] == "A":
                continue

            # add to set to prevent duplicates
            product_ids.add(product.attrib["id"])

ret_list = ["ServiceID"] + list(sorted(product_ids))
print(ret_list)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

python elementtree - how to find all elements in xml with certain attribute

Python XML ElementTree not reading node with &

XML Parsing with Python - find attribute value with Elementtree

Python, ElementTree: Find specific content in XML tag?

Python XML ElementTree Removing All Elements

Find all elements in ElementTree by attribute using Python

Getting all instances of child node using xml.etree.ElementTree

Getting all children of a node using xml.etree.ElementTree

Error while extracting XML-node text using elementtree in python

Get all xml attribute values in python3 using ElementTree

Get XML Parent ID and all Childs underneath using ElementTree and python

Python XML ElementTree - findall

Python XML PArse with ElementTree

Parsing XML in Python with ElementTree - findall

Parsing XML using Python ElementTree

Python 2.5: ElementTree and UML in XML

Python ElementTree xml output to csv

Python - Parsing XML data with ElementTree

Parsing XML in Python with ElementTree - findall()

Python ElementTree find() using namespaces

How to extract XML node values with Elementtree

Using ElementTree to find a node - invalid predicate

How to conditionally insert an attribute into a node in Python using xml.etree.ElementTree

xml.etree.ElementTree insert into child of child node creating infinity loop Python

XML Parsing in python(xml.etree.ElementTree)

ElementTree XML Parsing Python, Problem with XML structure

Find parent element of a 'title' xml tag containing specific text using Python ElementTree

Find the content of the xml tag in the same category as having another tag with Python elementTree module

Can't get find to access element in XPath in python xml.etree.ElementTree

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive