Setting an attribute value in XML file using ElementTree
Asked Answered
L

1

9

I need to change the value of an attribute named approved-by in an xml file from 'no' to 'yes'. Here is my xml file:

<?xml version="1.0" encoding="UTF-8" ?>
<!--Arbortext, Inc., 1988-2008, v.4002-->
<!DOCTYPE doc PUBLIC "-//MYCOMPANY//DTD XSEIF 1/FAD 110 05 R5//EN"
     "XSEIF_R5.dtd">
<doc version="XSEIF R5" xmlns="urn:x-mycompany:r2:reg-doc:1551-fad.110.05:en:*">
    <meta-data>
        <?Pub Dtl?>
            <confidentiality class="mycompany-internal" />
            <doc-name>INSTRUCTIONS</doc-name>
            <doc-id>
                <doc-no type="registration">1/1531-CRA 119 1364/2</doc-no>
                <language code="en" />
                <rev>PA1</rev>
                <date>
                    <y>2013</y>
                    <m>03</m>
                    <d>12</d>
                </date>
            </doc-id>
            <company-id>
                <business-unit></business-unit>
                <company-name></company-name>
                <company-symbol logotype="X"></company-symbol>
            </company-id>
            <title>SIM Software Installation Guide</title>
            <drafted-by>
                <person>
                    <name>Shahul Hameed</name>
                    <signature>epeeham</signature>
                </person>
            </drafted-by>
            <approved-by approved="no">
                <person>
                    <name>AB</name>
                    <signature>errrrrn</signature>
            </approved-by>

I tried in two ways, and failed in both. My first way is

import xml.etree.ElementTree as ET
from xml.etree.ElementTree import Element

root = ET.parse('Path/1_1531-CRA 119 1364_2.xml')
sh = root.find('approved-by')
sh.set('approved', 'yes')
print etree.tostring(root)

In this way, I got an error message saying AttributeError: 'NoneType' object has no attribute 'set'.

So I tried another way.

import xml.etree.ElementTree as ET
from xml.etree.ElementTree import Element

root = ET.parse('C:/Path/1_1531-CRA 119 1364_2.xml')
elem = Element("approved-by")
elem.attrib["approved"] = "yes"

I didn't get any error, also it didn't set the attribute either. I am confused, and not able to find whats wrong with this script.

Locoweed answered 29/7, 2013 at 10:48 Comment(0)
C
14

Since the xml you've provided is not valid, here's an example:

import xml.etree.ElementTree as ET


xml = """<?xml version="1.0" encoding="UTF-8"?>
<body>
    <approved-by approved="no">
        <name>AB</name>
        <signature>errrrrn</signature>
    </approved-by>
</body>
"""

tree = ET.fromstring(xml)
sh = tree.find('approved-by')
sh.set('approved', 'yes')

print ET.tostring(tree)

prints:

<body>
    <approved-by approved="yes">
        <name>AB</name>
        <signature>errrrrn</signature>
    </approved-by>
</body>

So, the first way you've tried works. Hope that helps.

Chacon answered 29/7, 2013 at 10:58 Comment(1)
:Yes indeed in this way. But if i try to parse an xml file, it throws me an error. The file has stylesheet, is it coz of tat?. You said the file is not valid, what does that mean?Locoweed

© 2022 - 2024 — McMap. All rights reserved.