簡體   English   中英

如何使用Python在現有XML中附加一堆標簽

[英]How to append bunch of tags in existing XML using Python

我有一個很大的xml文件(2000 kb +),我想使用Python附加。 我需要檢查是否存在帶有某些值的標記。 如果不是,我需要在文件的特定位置(標簽之前)添加一堆標簽

讓我們來看這個例子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>

第一步是檢查xml文件中是否存在French Toast。 如果沒有,請在</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>

試試下面給出的代碼

編輯:

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

碼:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM