2011-02-16 87 views
1

我想在python中解析xml,但是作为一个字符串,而不是从文件中取出。有人可以帮我做这个吗?python解析xml文本

回答

8

从一个文件,你可以正常做得一样

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) 
+0

非常感谢你 – rach 2011-02-16 02:50:42

3

你可以使用:xml.dom.minidom.parseString(text)

此方法创建一个字符串StringIO对象并传递到解析()。

您也可以使用相同的技术将StringIO用于需要类文件对象的任何其他XML解析器。

import StringIO 
your_favourite_xml_parser.parse(StringIO.StringIO('<xml>...</xml>')) 
2

可以使用(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 
1

您也可以使用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');