使用ElementTree在XML文件中设置属性值 [英] Setting an attribute value in XML file using ElementTree

查看:55
本文介绍了使用ElementTree在XML文件中设置属性值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要将XML文件中名为批准的的属性的值从否更改为是。这是我的xml文件:

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)

我收到一条错误消息,提示 AttributeError:'NoneType'对象没有属性'set'

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

所以我尝试了

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.

推荐答案

由于您提供的xml无效,例如:

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)

打印:

<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.

这篇关于使用ElementTree在XML文件中设置属性值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆