Python Minidom XML Query

ぐ巨炮叔叔 提交于 2019-12-01 21:21:49

问题


I'm trying to query this XML with lxml:

<lista_tareas>
    <tarea id="1" realizzato="False" data_limite="12/10/2012" priorita="1">
    <description>XML TEST</description>
</tarea>
<tarea id="2" realizzato="False" data_limite="12/10/2012" priorita="1">
    <description>XML TEST2</description>
</tarea>

I wrote this code:

from lxml import etree
doc = etree.parse(file_path)    

root = etree.Element("lista_tareas")

for x in root:
    z = x.Element("tarea")
    for y in z:
        element_text = y.Element("description").text
        print element_text

It doesn't print anything, could you suggest me how to do?


回答1:


You do not want to use the minidom; use the ElementTree API instead. The DOM API is a very verbose and constrained API, the ElementTree API plays to Python's strengths instead.

The MiniDOM module doesn't offer any query API like you are looking for.

You can use the bundled xml.etree.ElementTree module, or you could install lxml, which offers more powerful XPath and other query options.

import xml.etree.ElementTree as ET
root = ET.parse('document.xml').getroot()

for c in root.findall('./Root_Node[@id='1']/sub_node'):
    # Do something with c



回答2:


Using lxml:

from lxml import etree

doc = etree.parse ( source )    
for c in doc.xpath ( "//Root_Node[@id='1']" ):
  subnode = c.find ( "sub_node" )
  # ... etc ...


来源:https://stackoverflow.com/questions/12815637/python-minidom-xml-query

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!