Updating XML Elements and Attribute Values ​​Using Python etree

I am trying to use the Python 2.7 library ElementTreeto parse an XML file, then replace certain element attributes with test data, and then save it as a unique XML file.

My idea for the solution was to (1) get new data from the CSV file by reading the file into a line, (2) cut the line according to certain separator marks, (3) add to the list and then (4) use ElementTreeto update / Removing / replacing an attribute with a specific value from the list.

I looked in the documentation ElementTreeand saw the functions clear()and remove(), but I have no idea about the syntax for their adequate use.

The following is an example of an XML code to change - attributes with XXXXXneed to be replaced / updated:

<TrdCaptRpt RptID="10000001" TransTyp="0">
    <RptSide Side="1" Txt1="XXXXX">
        <Pty ID="XXXXX" R="1"/>
    </RptSide>
</TrdCaptRpt>

The expected result will be, for example:

<TrdCaptRpt RptID="10000001" TransTyp="0">
    <RptSide Side="1" Txt1="12345">
        <Pty ID="ABCDE" R="1"/>
    </RptSide>
</TrdCaptRpt>

How to use commands etreeto change the basic XML for updating using an element from the list []?

+6
source share
1 answer

For this kind of work, I always recommend BeautifulSoupit because it has a really easy to learn API:

from BeautifulSoup import BeautifulStoneSoup as Soup

xml = """
<TrdCaptRpt RptID="10000001" TransTyp="0">
    <RptSide Side="1" Txt1="XXXXX">
        <Pty ID="XXXXX" R="1"/>
    </RptSide>
</TrdCaptRpt>
"""

soup = Soup(xml)
rpt_side = soup.trdcaptrpt.rptside
rpt_side['txt1'] = 'Updated'
rpt_side.pty['id'] = 'Updated'

print soup

Output Example:

<trdcaptrpt rptid="10000001" transtyp="0">
<rptside side="1" txt1="Updated">
<pty id="Updated" r="1">
</pty></rptside>
</trdcaptrpt>

Edit: with, xml.etree.ElementTreeyou can use the following script:

from xml.etree import ElementTree as etree

xml = """
<TrdCaptRpt RptID="10000001" TransTyp="0">
    <RptSide Side="1" Txt1="XXXXX">
        <Pty ID="XXXXX" R="1"/>
    </RptSide>
</TrdCaptRpt>
"""

root = etree.fromstring(xml)
rpt_side = root.find('RptSide')
rpt_side.set('Txt1', 'Updated')
pty = rpt_side.find('Pty')
pty.set('ID', 'Updated')
print etree.tostring(root)

Output Example:

<TrdCaptRpt RptID="10000001" TransTyp="0">
    <RptSide Side="1" Txt1="Updated">
        <Pty ID="Updated" R="1" />
    </RptSide>
</TrdCaptRpt>
+12
source

All Articles