在不知道标签的情况下,Python如何在XML文件中搜索替换文本(标签值)
我刚开始学Python,想用一个XML文件。我知道怎么解析和查找信息,前提是我知道文件的结构,但我不知道怎么在不知道标签的情况下查找某个值。
比如说:
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>TRUE</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>TRUE</year>
<price>39.95</price>
</book>
<adventure>
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>TRUE</year>
<price>TRUE</price>
</adventure>
</bookstore>
在这个例子中,我想找到所有的“TRUE”值,并把这些值替换成“OK”。你会怎么做呢?
谢谢!
3 个回答
0
这是我所做的,目的是让我能找到我xml文件中的所有值。
for node in root.iter():
if (node.text != None):
node.text = search_in_dictonary_foot(">"+node.text+"<")
0
如果单词 TRUE
只出现在标签之间,你可以用简单的字符串替换来处理。
my_xml = """
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>TRUE</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>TRUE</year>
<price>39.95</price>
</book>
</bookstore>
"""
>>> my_xml.replace(">TRUE<",">OK<")
'\n<bookstore>\n <book category="COOKING">\n <title lang="en">Everyday Italian</title>\n <author>OK</author>\n <year>2005</year>\n <price>30.00</price>\n</book>\n <book category="CHILDREN">\n <title lang="en">Harry Potter</title>\n <author>J K. Rowling</author>\n <year>2005</year>\n <price>29.99</price>\n</book>\n<book category="WEB">\n <title lang="en">Learning XML</title>\n <author>Erik T. Ray</author>\n <year>OK</year>\n <price>39.95</price>\n </book>\n</bookstore>\n'
>>>
虽然这种方法没有用xml库那么强大,但应该能完成任务。
1
这里有一个使用标准库中的 xml.etree.ElementTree
的选项:
import xml.etree.ElementTree as ET
data = """xml here"""
tree = ET.fromstring(data)
for element in tree.getiterator():
if element.text == 'TRUE':
element.text = 'OK'
print ET.tostring(tree)
输出结果是:
<bookstore>
<book category="COOKING">
<title lang="en">Everyday Italian</title>
<author>OK</author>
<year>2005</year>
<price>30.00</price>
</book>
<book category="CHILDREN">
<title lang="en">Harry Potter</title>
<author>J K. Rowling</author>
<year>2005</year>
<price>29.99</price>
</book>
<book category="WEB">
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>OK</year>
<price>39.95</price>
</book>
<adventure>
<title lang="en">Learning XML</title>
<author>Erik T. Ray</author>
<year>OK</year>
<price>OK</price>
</adventure>
</bookstore>