Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

find xml element based on its attribute and change its value

I am using python xmlElementTree and want to assign or modify a xml element value based on its attribute. Can somebody give me an idea how to do this?

For example: Here is a xml file and I need to set the value for the element "number" based on the attribute "sys/phoneNumber/1", "sys2/SMSnumber/1" and so on.

<root>
    <phoneNumbers>
        <number topic="sys/phoneNumber/1" update="none" />
        <number topic="sys/phoneNumber/2" update="none" />
        <number topic="sys/phoneNumber/3" update="none" />
    </phoneNumbers>

    <gfenSMSnumbers>
        <number topic="sys2/SMSnumber/1" update="none" />
        <number topic="sys2/SMSnumber/2" update="none" />
    </gfenSMSnumbers>
</root>

edit: Added closure for the tag root in the XML file.

like image 665
user1282251 Avatar asked Mar 21 '12 01:03

user1282251


People also ask

How do you change a value in XML?

The way to change the value of an attribute, is to change its text value. This can be done using the setAttribute() method or setting the nodeValue property of the attribute node.

What is attribute value XML?

The XML attribute is a part of an XML element. The addition of attribute in XML element gives more precise properties of the element i.e, it enhances the properties of the XML element. In the above syntax element_name is the name of an element which can be any name.

What is the difference between XML element and attribute?

Attributes are part of XML elements. An element can have multiple unique attributes. Attribute gives more information about XML elements. To be more precise, they define properties of elements.


2 Answers

You can access the attribute value as this:

from elementtree.ElementTree import XML, SubElement, Element, tostring

text = """
<root>
    <phoneNumbers>
        <number topic="sys/phoneNumber/1" update="none" />
        <number topic="sys/phoneNumber/2" update="none" />
        <number topic="sys/phoneNumber/3" update="none" />
    </phoneNumbers>

    <gfenSMSnumbers>
        <number topic="sys2/SMSnumber/1" update="none" />
        <number topic="sys2/SMSnumber/2" update="none" />
    </gfenSMSnumbers>
</root>
"""

elem = XML(text)
for node in elem.find('phoneNumbers'):
    print node.attrib['topic']
    # Create sub elements
    if node.attrib['topic']=="sys/phoneNumber/1":
        tag = SubElement(node,'TagName')
        tag.attrib['attr'] = 'AttribValue'

print tostring(elem)

forget to say, if your ElementTree version is greater than 1.3, you can use XPath:

elem.find('.//number[@topic="sys/phoneNumber/1"]')

http://effbot.org/zone/element-xpath.htm

or you can use this simple one:

for node in elem.findall('.//number'):
    if node.attrib['topic']=="sys/phoneNumber/1":
        tag = SubElement(node,'TagName')
        tag.attrib['attr'] = 'AttribValue'
like image 123
focusheart Avatar answered Sep 22 '22 06:09

focusheart


For me this Elementtree snipped of code worked to find element by attribute:

import xml.etree.ElementTree as ET
tree = ET.parse('file.xml')
root = tree.getroot()


topic=root.find(".//*[@topic='sys/phoneNumber/1']").text
like image 25
Eduard Florinescu Avatar answered Sep 25 '22 06:09

Eduard Florinescu