简体   繁体   English

如何使用python修改XML中的元素文本

[英]How to modify element text in XML using python

Would you give me some advice how to modify element text in XML using python?你能给我一些建议如何使用 python 修改 XML 中的元素文本吗? if I want to insert other text in front of text of the first BBB element, which part should i change at the code below?如果我想在第一个 BBB 元素的文本前面插入其他文本,我应该在下面的代码中更改哪一部分?

Please don't use fromstring and other modules(example lxml).请不要使用 fromstring 和其他模块(例如 lxml)。

This is sample XML below.这是下面的示例 XML。

<?xml version="1.0"?>
<data>
    <AAA>
        <CCC>
            <BBB>This</BBB> ----> the first BBB element
        </CCC>
        <CCC>  
            <BBB>is</BBB>
        </CCC>
        <CCC>
            <BBB>test1</BBB>
        </CCC>
    </AAA>

    <AAA>
        <CCC>
            <BBB>This is test</BBB>
        </CCC>
    </AAA>
</data>

and it's code what i'm am trying below.它是我在下面尝试的代码。

import xml.etree.ElementTree as ET
import re

tree = ET.parse("C:\\test\\python test\\data_text.xml")
root = tree.getroot()                                                

for AAA in root.findall('AAA'):
    for CCC in AAA.findall('CCC'):
        for BBB in CCC.findall('BBB')[0]:
            BBB_text = '11111' + BBB.text
            print(BBB_text)

tree.write('C:\\test\\python test\\output.xml')

As far as i know, for BBB in CCC.findall('BBB')[0]:据我所知,对于 CCC.findall('BBB')[0] 中的 BBB:

[0] means find only the first BBB, but i guess it's wrong. [0] 表示只找到第一个 BBB,但我猜这是错误的。

and this is the result that i want.这就是我想要的结果。

<?xml version="1.0"?>
<data>
    <AAA>
        <CCC>
            <BBB>11111This</BBB> ----> the first BBB element
        </CCC>
        <CCC>  
            <BBB>is</BBB>
        </CCC>
        <CCC>
            <BBB>test1</BBB>
        </CCC>
    </AAA>

    <AAA>
        <CCC>
            <BBB>This is test</BBB>
        </CCC>
    </AAA>
</data>

You do not need to iterate all the tags if you just need to update a single tag.如果您只需要更新单个标签,则无需迭代所有标签。

Try:尝试:

import xml.etree.ElementTree as ET

tree = ET.parse(filename)
root = tree.getroot()                                                

for AAA in root.findall('AAA'):
    if AAA.find('CCC'):
        BBB = AAA.find('CCC').find('BBB')
        BBB.text = '33333' + BBB.text

tree.write('C:\\test\\python test\\output.xml')

ElementTree supports a limited sub-set of XPath . ElementTree支持XPath 的有限子集

You can use您可以使用

bbb = tree.find("./AAA/CCC/BBB")
if bbb:
    # do something   

to get the very first such node in the tree, or获得树中第一个这样的节点,或者

for bbb in tree.iterfind("./AAA/CCC/BBB"):
    # do something

to iterate over all of them.遍历所有这些。

Disclaimer: XPath answer from @Tomalak is way more elegant!免责声明: @Tomalak 的 XPath 回答更加优雅!


After some tests, it looks like CCC.findall('BBB')[0] works fine.经过一些测试,看起来CCC.findall('BBB')[0]工作正常。 Since you want the first BBB tag within the document and not within each AAA tag, I would loose the for loops and modify the bit from my comment.由于您希望文档中的第一个BBB标记而不是每个AAA标记中的标记,因此我会松开for循环并修改我的注释中的位。 I got this:我懂了:

import xml.etree.ElementTree as ET
import re

tree = ET.parse("data_text.xml")
root = tree.getroot()                                                

AAA = root.find('AAA')
CCC = AAA.find('CCC')
BBB = CCC.find('BBB')
BBB.text = '11111' + BBB.text
print(BBB.text)

tree.write('output.xml')

Seems to do the trick.似乎可以解决问题。 You may need to check the validity of AAA , BBB and CCC to avoid crashes if the XML does not contain such tags.如果 XML 不包含此类标签,您可能需要检查AAABBBCCC的有效性以避免崩溃。

Hope this helps.希望这可以帮助。

Well you can do it like this:那么你可以这样做:

for a in tree:
    for c in a:
        for b in c:
            b.text = '11111' + b.text
            break
        break
    break

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM