[英]How to append bunch of tags in existing XML using Python
I have a big xml file (2000 kb+) that I want to append using Python. 我有一个很大的xml文件(2000 kb +),我想使用Python附加。 I need to check to see if a tag with some value exists. 我需要检查是否存在带有某些值的标记。 If not I need to add a bunch of tags to the file at a particular position (before a tag) 如果不是,我需要在文件的特定位置(标签之前)添加一堆标签
Lets take this example, restaurant.xml: 让我们来看这个例子restaurant.xml:
<restaurant>
<menu>
<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description> Two of our famous Belgian Waffles with plenty of real maple syrup</description>
<calories>650</calories>
</food>
<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>Light Belgian waffles covered with strawberries and whipped cream</description>
<calories>900</calories>
</food>
<food>
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<description>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
<calories>900</calories>
</food>
</breakfast_menu>
<lunch_menu>
blah...blah...blah
</menu>
</restaurant>
1st step is to check if French Toast exists in xml file. 第一步是检查xml文件中是否存在French Toast。 If not, add the following before </breakfast_menu>
如果没有,请在</breakfast_menu>
之前添加以下内容
<food>
<name>French Toast</name>
<price>$4.50</price>
<description>Thick slices made from our homemade sourdough bread</description>
<calories>600</calories>
</food>
Try the code given below 试试下面给出的代码
EDIT: 编辑:
<restaurant>
<menu>
<breakfast_menu>
<food>
<name>Belgian Waffles</name>
<price>$5.95</price>
<description> Two of our famous Belgian Waffles with plenty of real maple syrup</description>
<calories>650</calories>
</food>
<food>
<name>Strawberry Belgian Waffles</name>
<price>$7.95</price>
<description>Light Belgian waffles covered with strawberries and whipped cream</description>
<calories>900</calories>
</food>
<food>
<name>Berry-Berry Belgian Waffles</name>
<price>$8.95</price>
<description>Light Belgian waffles covered with an assortment of fresh berries and whipped cream</description>
<calories>900</calories>
</food>
</breakfast_menu>
</menu>
</restaurant>
Code: 码:
import xml.etree.ElementTree as ET
def SubElementWithText(parent, tag, text):
attrib = {}
element = parent.makeelement(tag, attrib)
parent.append(element)
element.text = text
return element
def addXml(file, xpath_to_check, xpath_to_add_element, nodedict):
tree = ET.parse(file)
root = tree.getroot()
if root.findall(xpath_to_check):
st = False
for elem in root.findall(xpath_to_check):
if elem.text == nodedict['name']:
st = True
if st == False:
breakfast_menu = root.find(xpath_to_add_element)
food = ET.SubElement(breakfast_menu, 'food')
for element, value in nodedict.items():
SubElementWithText(food, element, value)
tree.write(file)
print("Elements added successfully")
else:
print("ELement already existed")
file = "restaurent.xml"
xpath_to_check = ".//food/name"
xpath_to_add_element = "./menu/breakfast_menu"
nodedict = {"name" : "French Toast", "price" : "$4.50", "description" : "Thick slices made from our homemade sourdough bread" , "calories" : "600" }
addXml(file, xpath_to_check, xpath_to_add_element, nodedict)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.