How to read and input values into XML with multiples nodes being named the same value?

Hi,
I have an XML file that I am working with that contains multiples nodes of the same name… So I am struggling to read from these fields as my current code inputs only in the first of these values.

Example:

<E1IDB02 SEGMENT="1">
    <FIIQUALI>BA</FIIQUALI>
    <FIIBKENN>Test</FIIBKENN>
</E1IDB02>
<E1IDB02 SEGMENT="1">
    <FIIQUALI>BB</FIIQUALI>
    <FIIBKENN>Test</FIIBKENN>
</E1IDB02>

I put the value of an XML file into a Tree

My current code :
Put “NewTest” into node “//[name()=‘E1IDB02’]/[name()=‘FIIBKENN’]” of Tree

This only affects the first of the 2 nodes values…

Example output:

<E1IDB02 SEGMENT="1"><FIIQUALI>BA</FIIQUALI><FIIBKENN>NewTest</FIIBKENN></E1IDB02>
<E1IDB02 SEGMENT="1"><FIIQUALI>BB</FIIQUALI><FIIBKENN>Test</FIIBKENN></E1IDB02>

Question:
How do I affect and input values into the second value of FIIBKENN?
The only difference for these values is the FIIQUALI value, which will either be BA or BB.

To update the values of all the FIIBKENN nodes, you can use a loop to iterate over all the E1IDB02 nodes and update the corresponding FIIBKENN nodes. Here’s an example:

from lxml import etree

# Assuming you have the XML data in a string variable named 'xml_data'
root = etree.fromstring(xml_data)

# Loop through all the E1IDB02 nodes
for e1idb02 in root.findall('E1IDB02'):
    # Update the FIIBKENN value
    e1idb02.find('FIIBKENN').text = 'NewTest'

# Convert the modified XML tree back to a string
updated_xml = etree.tostring(root, encoding='utf-8').decode('utf-8')
from lxml import etree

# Assuming you have the XML data in a string variable named 'xml_data'
root = etree.fromstring(xml_data)

# Loop through all the E1IDB02 nodes
for e1idb02 in root.findall('E1IDB02'):
    # Update the FIIBKENN value
    e1idb02.find('FIIBKENN').text = 'NewTest'

# Convert the modified XML tree back to a string
updated_xml = etree.tostring(root, encoding='utf-8').decode('utf-8')

In this code, we’re using the findall() method to get all the E1IDB02 nodes, and then we’re using the find() method to get the FIIBKENN node for each E1IDB02 node. We then update the text of the FIIBKENN node to ‘NewTest’.

Finally, we convert the modified XML tree back to a string using the etree.tostring() function.

The resulting updated_xml variable will contain the modified XML data with all the FIIBKENN values updated to ‘NewTest’.

If you want to update the FIIBKENN values based on the FIIQUALI value, you can modify the code as follows:

from lxml import etree

# Assuming you have the XML data in a string variable named 'xml_data'
root = etree.fromstring(xml_data)

# Loop through all the E1IDB02 nodes
for e1idb02 in root.findall('E1IDB02'):
    # Get the FIIQUALI value
    fiiquali = e1idb02.find('FIIQUALI').text

    # Update the FIIBKENN value based on the FIIQUALI value
    if fiiquali == 'BA':
        e1idb02.find('FIIBKENN').text = 'NewTest'
    elif fiiquali == 'BB':
        e1idb02.find('FIIBKENN').text = 'OtherTest'

# Convert the modified XML tree back to a string
updated_xml = etree.tostring(root, encoding='utf-8').decode('utf-8')

In this modified version, we’re checking the FIIQUALI value for each E1IDB02 node and updating the FIIBKENN value accordingly. If FIIQUALI is ‘BA’, we set FIIBKENN to ‘NewTest’, and if FIIQUALI is ‘BB’, we set FIIBKENN to ‘OtherTest’.

Hi,
Thanks for the in depth response! Fortunately I was able to find a much easier method of obtaining the 2nd value : Put “NewTest” into node “//[name()=‘E1IDB02’][2]/ [name()=‘FIIBKENN’]” of Tree

Notice I specified the index of which node I would use “[2]”.