简体   繁体   English

如何使用python替换xml的两个标签之间的文本?

[英]How to replace text between two tags of xml using python?

Below is the example xml file. 以下是示例xml文件。

    <?xml version='1.0' encoding='UTF-8'?>
    <a>
        <b>
            <c>
                <d>TEXT</d>
            </c>  
       </b>
    </a>

I need to Replace "TEXT" with list of strings so that my xml look like below. 我需要用字符串列表替换“ TEXT”,以便我的xml如下所示。

    <?xml version='1.0' encoding='UTF-8'?>
    <a>
        <b>
            <c>
                <d>TEXT1,TEXT2,TEXT3</d>
            </c>  
       </b>
    </a>

Please tell me how can i achieve this using python. 请告诉我如何使用python实现此目的。

Try this: 尝试这个:

a = a.replace(<old string>, <new string>)

read file and do this operation. 读取文件并执行此操作。

this should work, 这应该工作,

from xml.dom import minidom
doc = minidom.parse('my_xml.xml')
item = doc.getElementsByTagName('d')
print item[0].firstChild.nodeValue
item[0].firstChild.replaceWholeText('TEXT, TEXT1 , etc...')

for s in item: #if you want to loop try this
    s.firstChild.replaceWholeText('TEXT, TEXT1 , etc...')

You can use lxml but it depends on your actual purpose of use, here is an example: 您可以使用lxml但这取决于您的实际使用目的,下面是一个示例:

from lxml import etree

a = '''<?xml version='1.0' encoding='UTF-8'?>
<a>
    <b>
        <c>
            <d>TEXT</d>
        </c>  
   </b>
</a>'''

tree = etree.fromstring(a)
#for file you need to use tree = etree.parse(filename)
for item in tree:
    for data in item:
        for point in data:
            if point.tag == 'd':
                if point.text == 'TEXT':
                    point.text = 'TEXT,TEXT,TEXT'
print(etree.tostring(tree))
#<a>
#    <b>
#        <c>
#            <d>TEXT,TEXT,TEXT</d>
#        </c>  
#   </b>
#</a>

You can treat an xml file just as a text file and use the functions you would use to manipulate strings. 您可以将xml文件视为文本文件,并使用处理字符串的功能。 For example: 例如:

with open('testxml.xml','r') as f:
    contents=f.read() #open xml file

stringlist=['Text1','Text2','Text3'] #list of strings you want to replace with
opentag='<d>' #tag in which you want to replace text
closetag='</d>'

oldtext=contents[contents.find(opentag)+3:contents.find(closetag)] 
newtext=''.join(str_+',' for str_ in stringlist)[:-1] #ignore last comma
contents=contents.replace(oldtext,newtext) #replace old text with new

with open('testxml.xml','w') as f:
    f.write(contents) #write contents to file

There might be many instances where you have a lot of nested tags and this simple script would not work. 在许多情况下,您会有很多嵌套标签,而这个简单的脚本将无法工作。 You could use Python's built in XML editing package ElementTree if you want to do more advanced tasks. 如果您想执行更多高级任务,则可以使用Python的内置XML编辑包ElementTree

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

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