python解析xml文本
答
从一个文件,你可以正常做得一样
from xml.dom import minidom
xmldoc = minidom.parse('~/diveintopython/common/py/kgp/binary.xml')
对于字符串,你可以把它改成
from xml.dom import minidom
xmldoc = minidom.parseString(Your string goes here)
答
你可以使用:xml.dom.minidom.parseString(text)
此方法创建一个字符串StringIO对象并传递到解析()。
您也可以使用相同的技术将StringIO用于需要类文件对象的任何其他XML解析器。
import StringIO
your_favourite_xml_parser.parse(StringIO.StringIO('<xml>...</xml>'))
答
可以使用(xml.etree.cElementTree)也。
import xml.etree.cElementTree as ET
aElement = ET.fromstring('<Root id="UUID_1"><Item id="id_Item" /></Root>')
See Python help document
Each element has a number of properties associated with it:
a tag which is a string identifying what kind of data this element represents (the element type, in other words).
a number of attributes, stored in a Python dictionary.
a text string.
an optional tail string.
a number of child elements, stored in a Python sequence
答
您也可以使用lxml。我的初创公司(http://dealites.com)每天都会涉及很多XML处理。我已经尝试了python中几乎所有可用的xml库。 lxml是可用于xml处理的最佳库。
你也可以尝试美丽的汤。它非常适合HTML解析,但是可以替代lxml。
LXML例如:
from lxml import etree;
parsedfeed = etree.xml('your xml here');
美丽的汤例如:
from BeautifulSoup import BeautifulStoneSoup;
soup = BeautifulStoneSoup('your xml here');
非常感谢你 – rach 2011-02-16 02:50:42