1

This is my XML document, I am trying to add an attribute to item element, in the PyDev debugger in eclipse I see the attribute is added, but once I check the new tree the attribute is not added.

Here is the XML:

<root>
    <login branch="99">
        <command action="create">
            <goods_transfer type="9" book="true" nummer="692" branch_from="99" branch_to="90" CheckQty="0">
                <item article="100500213" main_price="49.950" EAN="5018746059881" amount="1.000" DateTime="20161202112913">
                    <size amount="1.000" index="59" EAN="5018746059881" Name="S 37/38" DateTime="20161202112913">
                    </size>
                </item>
                <item article="100500213" main_price="49.950" EAN="5018746059898" amount="2.000" DateTime="20161202112914">
                    <size amount="2.000" index="60" EAN="5018746059898" Name="M 39/40" DateTime="20161202112914">
                    </size>
                </item>
            </goods_transfer>
        </command>
    </login>
</root>

Here is my code using Python 3.4 from Anaconda:

with open(fileName, 'r+b') as f:
    tree = etree.parse(f)
    for _,element in etree.iterparse(f, tag='item'):
#After this line is executed I see the attribute is added
        element.attrib['DocumentCode'] = 'the value of the attr'
        element.clear()
#When I check the new file the attribute is not added
    tree.write(fileName)

What I aim for is this:

                <item article="100500213" main_price="49.950" EAN="5018746059881" amount="1.000" DateTime="20161202112913" DocumentCode='the value of the attr'>
                    <size amount="1.000" index="59" EAN="5018746059881" Name="S 37/38" DateTime="20161202112913">
                    </size>
                </item>
3
  • can you give an example of required output? Commented Dec 3, 2016 at 13:30
  • I edited the answer, added the example output I am looking for, please check the item element, the new attribute is the last one I am trying to add. Commented Dec 3, 2016 at 13:37
  • 3
    Remove the line : element.clear(). "Resets an element. This function removes all subelements, clears all attributes and sets the text and tail properties to None." [documentation] Commented Dec 3, 2016 at 14:04

2 Answers 2

1

This code should work as you want:

from io import StringIO
from lxml import etree


fileName = '...'
context = etree.iterparse(fileName, tag='item')

for _, element in context:
    element.attrib['DocumentCode'] = 'the value of the attr'

with open(fileName, 'wb') as f:
    f.write(etree.tostring(context.root, pretty_print=True))
Sign up to request clarification or add additional context in comments.

1 Comment

Little reminder: stackoverflow.com/help/someone-answers, if the answer is good for you :)
0
import xml.etree.ElementTree as et
with open(filename, 'r+b') as f:
    tree = et.parse(f):
    [tr.attrib.update({"aaaa":"bbbb"}) for tr in tree.iter() if tr.tag== "item"]
    tr.write(output)

Here is possibility to do it with standart lib.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.