python xml
来源:互联网 发布:数据库结构的数据模型 编辑:程序博客网 时间:2024/06/05 17:15
These days, the most popular (and very simple) option is the ElementTree API, which has been included in the standard library since Python 2.5.
The available options for that are:
ElementTree (Basic, pure-Python implementation of ElementTree. Part of the standard library since 2.5)
cElementTree (Optimized C implementation of ElementTree. Also offered in the standard library since 2.5)
LXML (Based on libxml2. Offers a rich superset of the ElementTree API as well XPath, CSS Selectors, and more)
Here’s an example of how to generate your example document using the in-stdlib cElementTree:
import xml.etree.cElementTree as ET
root = ET.Element(“root”)
doc = ET.SubElement(root, “doc”)
ET.SubElement(doc, “field1”, name=”blah”).text = “some value1”
ET.SubElement(doc, “field2”, name=”asdfasd”).text = “some vlaue2”
tree = ET.ElementTree(root)
tree.write(“filename.xml”)
I suggest ElementTree. There are other compatible implementations of the same API, such as lxml, and cElementTree in the Python standard library itself; but, in this context, what they chiefly add is even more speed – the ease of programming part depends on the API, which ElementTree defines.
After building an Element instance e from the XML, e.g. with the XML function, or by parsing a file with something like
import xml.etree.ElementTree
e = xml.etree.ElementTree.parse(‘thefile.xml’).getroot()
or any of the many other ways shown at ElementTree, you just do something like:
for atype in e.findall(‘type’):
print(atype.get(‘foobar’))
and similar, usually pretty simple, code patterns.
Seems that ElementTree has some vulnerability issues, this is a quote from the docs: Warning The xml.etree.ElementTree module is not secure against maliciously constructed data. If you need to parse untrusted or unauthenticated data see XML vulnerabilities.
- Python & XML
- Python & XML
- python xml
- python xml
- Python XML
- python xml
- python xml
- xml python
- python xml
- Python---xml
- python xml
- python xml
- python 解析XML xml.dom
- python 解析XML xml.dom
- python 解析XML xml.dom
- python解析xml(创建xml)
- python 解析XML xml.dom
- python 解析XML xml.dom
- leetcode题解-31. Next Permutation
- pandas操作
- Windows10资源管理器占用CPU过高解决办法
- Codeforces 4D Mysterious Present
- (转)java正则表达式相关知识
- python xml
- 最短路(1)--dijkstra
- [AHOI2006] BZOJ1266 上学路线route-图论-最短路树-最小割
- 最长上升子序列(动态规划)
- java冒泡排序和选择排序和系统自动排序
- 进程与线程
- 对原型对象中this的一个认知--(转自张鑫旭)
- Java/Web调用Hadoop进行MapReduce示例
- oracle的幸福之旅