我想在python中解析xml,但是作爲一個字符串,而不是從文件中取出。有人可以幫我做這個嗎?python解析xml文本
1
A
回答
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)
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');
相關問題
- 1. 的Python解析XML文件
- 2. Python XML解析
- 3. Python - XML解析
- 4. 解析XML Python
- 5. 將XML /文本文件解析爲XML?
- 6. 解析XML文本列表
- 7. VB.NET XML文本解析
- 8. 用jQuery解析XML文本
- 9. Python ElementTree XML解析
- 10. XML在python解析
- 11. Python BeautifulSoup XML解析
- 12. 用Python解析XML
- 13. Python的XML解析
- 14. 的Python解析XML
- 15. 使用XML解析Python XML
- 16. Python的 - 解析文本
- 17. 用Python解析文本
- 18. Python文本解析&分裂
- 19. python中的文本解析
- 20. Python ElementTree多解析XML解析
- 21. 從本地xml解析XML
- 22. 解析包含Python中的XML條目的文本/ csv文件
- 23. 解析XML文件時編輯文本
- 24. 外來文化XML文本解析
- 25. 解析與Python的XML文件
- 26. 使用python解析XML文件
- 27. 使用python解析XML文件
- 28. 的Python解析XML/OPF文件
- 29. 使用python創建/解析xml文件
- 30. Python - 從XML中解析單行文件
非常感謝你 – rach 2011-02-16 02:50:42