[英]How to (push) parse XML files in Python?
I've already seen this question , but it's from the 2009. 我已经看到了这个问题 ,但这是从2009年开始的。
What's a simple modern way to handle XML files in Python 3? 在Python 3中处理XML文件的简单现代方法是什么?
Ie, from this TLD (adapted from here ): 即,根据此TLD(从此处改编):
<?xml version="1.0" encoding="UTF-8" ?>
<taglib>
<tlib-version>1.0</tlib-version>
<short-name>bar-baz</short-name>
<tag>
<name>present</name>
<tag-class>condpkg.IfSimpleTag</tag-class>
<body-content>scriptless</body-content>
<attribute>
<name>test</name>
<required>true</required>
<rtexprvalue>true</rtexprvalue>
</attribute>
</tag>
</taglib>
I want to parse TLD files (Java Server Pages Tag Library Descriptors), to obtain some sort of structure in Python (I have still to decide about that part). 我想解析TLD文件(Java服务器页面标签库描述符),以获得Python中的某种结构(我仍然需要决定那部分)。
Hence, I need a push parser. 因此,我需要一个推送解析器。 But I won't do much more with it, so I'd rather prefer a simple API (I'm new to Python). 但是我不会做更多的事情,所以我宁愿使用一个简单的API(我是Python的新手)。
xml.etree.ElementTree
is still there, in the standard library: xml.etree.ElementTree
仍在标准库中:
import xml.etree.ElementTree as ET
data = """your xml here"""
tree = ET.fromstring(data)
print(tree.find('tag/name').text) # prints "present"
If you look outside of the standard library, there is a very popular and fast lxml
module that follows the ElementTree
interface and supports Python3: 如果您在标准库之外看,有一个非常流行且快速的lxml
模块,该模块遵循ElementTree
接口并支持Python3:
from lxml import etree as ET
data = """your xml here"""
tree = ET.fromstring(data)
print(tree.find('tag/name').text) # prints "present"
Besides, there is lxml.objectify
that allows you to deal with XML structure like with a Python object. 此外,还有lxml.objectify
,它允许您像处理Python对象一样处理XML结构。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.