Python使用SAX解析xml文件
- 这里要解析一个早餐菜单的xml文件,包含了
name
,price
,description
,calories
。
large_intestine.xml
<breakfast_menu year="2023">
<food>
<name>Belgian Waffles</name>
<price>$6.3</price>
<description>two of our famous Belgian Waffles with plenty of real maple syrup</description>
<calories>650</calories>
</food>
<food>
<name>九转大肠</name>
<price>$9.9</price>
<description>我去除了大部分的腥味,但在清洗的过程中故意保留了一部分</description>
<calories>999</calories>
</food>
</breakfast_menu>
解析代码如下:
xml_parse.py
import xml.sax
class MenuHandler(xml.sax.ContentHandler):
def __init__(self):
self.curData = ""
self.name = ""
self.price = ""
self.description = ""
self.calories = ""
def startElement(self, tag, attributes):
self.curData = tag
if self.curData == "breakfast_menu":
print("This is a breakfast menu")
year = attributes["year"]
print(f"年份:{year}")
print()
if self.curData == "food":
print("---------------------------")
def characters(self, content):
if self.curData == "name":
self.name = content
elif self.curData == "price":
self.price = content
elif self.curData == "description":
self.description = content
elif self.curData == "calories":
self.calories = content
else:
pass
def endElement(self, tag):
if self.curData == "name":
print(f"name: {self.name}")
elif self.curData == "price":
print(f"price: {self.price}")
elif self.curData == "description":
print(f"description: {self.description}")
elif self.curData == "calories":
print(f"calories: {self.calories}")
else:
pass
self.curData = ""
if __name__ == "__main__":
parser = xml.sax.make_parser()
handler = MenuHandler()
parser.setContentHandler(handler)
parser.parse("large_intestine.xml")
执行结果如下: