Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在python中从XML中提取项目列表_Python_Xml_Lxml - Fatal编程技术网

在python中从XML中提取项目列表

在python中从XML中提取项目列表,python,xml,lxml,Python,Xml,Lxml,在python中,从以下xml中提取项目列表的最佳方法是什么 <iq xmlns="jabber:client" to="__anonymous__admin@localhost/8978528613056092673206" from="conference.localhost" id="disco" type="result"> <query xmlns="http://jabber.org/protocol/disco#items"> &

在python中,从以下xml中提取项目列表的最佳方法是什么

<iq xmlns="jabber:client" to="__anonymous__admin@localhost/8978528613056092673206" 
 from="conference.localhost" id="disco" type="result">
    <query xmlns="http://jabber.org/protocol/disco#items">
        <item jid="pgatt@conference.localhost" name="pgatt (1)"/>
        <item jid="pgatt@conference.localhost" name="pgatt (1)"/>
    </query>
</iq>

我通常将lxml与xpath一起使用,但在这种情况下它不起作用。我认为我的问题是由于名称空间。我没有设置lxml,我愿意使用任何库


我想要一个足够健壮的解决方案,如果xml的一般结构发生变化,它可能会失败。

我不确定
lxml
,但是您可以使用类似
/*[local-name()=“item”]
的表达式来拉出
item
元素,而不管它们的名称空间如何

您可能还想了解一下XML处理的方法

>>> import amara.bindery
>>> doc = amara.bindery.parse(
...     '''<iq xmlns="jabber:client" 
...          to="__anonymous__admin@localhost/8978528613056092673206"
...          from="conference.localhost" id="disco" type="result">
...          <query xmlns="http://jabber.org/protocol/disco#items">
...            <item jid="pgatt@conference.localhost" name="pgatt (1)"/>
...            <item jid="pgatt@conference.localhost" name="pgatt (1)"/>
...          </query>
...        </iq>''')
>>> for item in doc.iq.query.item:
...   print item.jid, item.name
...
pgatt@conference.localhost pgatt (1)
pgatt@conference.localhost pgatt (1)
>>>
导入amara.bindery >>>doc=amara.bindery.parse( ... ''' ... ... ... ... ... ''') >>>对于doc.iq.query.item中的项目: ... 打印item.jid、item.name ... pgatt@conference.localhostpgatt(1) pgatt@conference.localhostpgatt(1) >>>
<我发现Amara后,我再也不考虑处理XML了。

< p>我回答了一个类似的问题,关于如何解析和搜索XML数据。

您需要查看xml2json函数。 该函数需要一个minidom对象。我就是这样得到我的xml的,不知道你是怎么做到的

from xml.dom import minidom
x = minidom.parse(urllib.urlopen(url))
json = xml2json(x)
或者,如果您使用的是字符串而不是url:

x = minidom.parseString(xml_string)
json = xml2json(x)

然后xml2json函数将返回一个包含xml中所有值的字典。您可能需要尝试并打印输出,以查看布局的外观。

我错过了机会,但这里介绍了如何在关注名称空间的同时做到这一点

您可以在查询中把它们全部拼出来,也可以创建一个名称空间映射,并将其传递给xpath查询

from lxml import etree

data = """<iq xmlns="jabber:client" to="__anonymous__admin@localhost/8978528613056092673206"
 from="conference.localhost" id="disco" type="result">
    <query xmlns="http://jabber.org/protocol/disco#items">
        <item jid="pgatt@conference.localhost" name="pgatt (1)"/>
        <item jid="pgatt@conference.localhost" name="pgatt (1)"/>
    </query>
</iq>"""

nsmap = {
  'jc': "jabber:client",
  'di':"http://jabber.org/protocol/disco#items"
}

doc = etree.XML(data)

for item in doc.xpath('//jc:iq/di:query/di:item',namespaces=nsmap):
  print etree.tostring(item).strip()
  print "Name: %s\nJabberID: %s\n" % (item.attrib.get('name'),item.attrib.get('jid'))
从lxml导入etree
data=”“”
"""
nsmap={
'jc':“jabber:client”,
"di":"http://jabber.org/protocol/disco#items"
}
doc=etree.XML(数据)
对于doc.xpath('//jc:iq/di:query/di:item',namespaces=nsmap)中的项:
打印etree.tostring(项).strip()
打印“名称:%s\n缩写:%s\n”%(item.attrib.get('Name'),item.attrib.get('jid'))
产生:

<item xmlns="http://jabber.org/protocol/disco#items" jid="pgatt@conference.localhost" name="pgatt (1)"/>
Name: pgatt (1)
JabberID: pgatt@conference.localhost

<item xmlns="http://jabber.org/protocol/disco#items" jid="pgatt@conference.localhost" name="pgatt (1)"/>
Name: pgatt (1)
JabberID: pgatt@conference.localhost

姓名:pgatt(1)
叽叽喳喳地说:pgatt@conference.localhost
姓名:pgatt(1)
叽叽喳喳地说:pgatt@conference.localhost

也许你应该看看你想提取哪些信息?谢谢<代码>/*[local-name()=“item”]正是我所需要的。有趣的是,你有什么理由抱怨这个库的速度吗?@MattH-不太好。速度不是很快,但到目前为止我还没有收到任何抱怨。由于使用方便,我将在返回任何其他LIB之前找到利用缓存的方法。