Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.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.etree.ElementTree解析XML文件时出现问题_Python_Xml_Python 3.x_Xml Parsing_Elementtree - Fatal编程技术网

Python 使用XML.etree.ElementTree解析XML文件时出现问题

Python 使用XML.etree.ElementTree解析XML文件时出现问题,python,xml,python-3.x,xml-parsing,elementtree,Python,Xml,Python 3.x,Xml Parsing,Elementtree,我必须解析包含如下条目的xml文件 <error code="UnknownDevice"> <description /> </error> 如何获取errorDef的值?我指的是id和description的值 如何使用未知设备搜索和提取这些值 [更新]错误组有不同的名称,但总是采用“XXX错误定义”、“YYY错误定义”等格式 此外,它们似乎嵌套在不同文档的不同深度 给定错误的标题,例如“unknownDevice”,如何搜索根目录下的所有内容

我必须解析包含如下条目的xml文件

<error code="UnknownDevice">
    <description />
</error>
如何获取
errorDef
的值?我指的是
id
description
的值

如何使用
未知设备
搜索和提取这些值


[更新]错误组有不同的名称,但总是采用“XXX错误定义”、“YYY错误定义”等格式

此外,它们似乎嵌套在不同文档的不同深度

给定错误的标题,例如“unknownDevice”,如何搜索根目录下的所有内容以获得相应的
id
描述值?


我可以使用“unknownDevice”直接访问它们吗?或者我必须先搜索错误组吗?

您需要一个选择器,尽管我不确定您是否可以使用lxml执行此操作。它有css选择器,但我在文档中找不到任何可以选择“id”的东西。。。 我只使用lxml来删除/添加html中的内容。也许可以看看刮痧?使用scrapy加载html时会是这样的

response.xpath('//div[@id="0x11"]/text()').extract()

首先,将错误定义解析到字典中:

errors = {
    errordef.attrib["name"]: {"id": errordef.attrib.get("id"), "description": errordef.findtext("description")}
    for errordef in root.xpath(".//group[@name='error definitions']/errordef[@name]")
}
然后,每次需要获取错误id和描述时,请按代码查找:

error_code = root.find("error").attrib["code"]
print(errors.get(error_code, "Unknown Error"))
请注意,
xpath()
方法来自
lxml.etree
。如果您使用的是
xml.etree.ElementTree
,请将
xpath()
替换为
findall()
xml.etree.ElementTree
提供的有限xpath支持足以支持所提供的表达式。

如果您有以下情况:

<group name="error definitions">
     <errordef id="0x11" name="UnknownDevice">
        <description>Indicated device is unknown</description>
     </errordef>
     ...
</group>
这会给你一些类似的东西:

0x11 Indicated device is unknown

要获取每个errordef元素的description和id值,可以执行以下操作:

for err in tree.xpath('//errordef'):
    print err.get('id'), err.find('description').text
import xml.etree.ElementTree as ET
dict01={}
tree=ET.parse('grpError.xml')
root=tree.getroot()
print (root)
docExe=root.findall('errordef') #Element reference
dict01=docExe[0].attrib #Store Attributes in dictionary
print (dict01)
print (dict01['id']) #Attributes of an element
print (dict01['name']) #Attributes of an element
print (docExe[0].find('description').text) #Child Elements inside parent Element
输出为:

<Element 'group' at 0x000001A582EDB4A8>
{'id': '0x11', 'name': 'UnknownDevice'}
0x11
UnknownDevice
Indicated device is unknown

{'id':'0x11','name':'UnknownDevice'}
0x11
未知设备
指示的设备未知

您正在使用lxml吗?所有当前的答案都假设(它们使用的是
xpath
方法,在lxml中可用,但在std-lib-ElementTree模块中不可用)。不,我不是(我太新了,甚至不知道它是什么)。当我回到办公室时,我会更新这个问题,以显示什么是
ET
。谢谢你指出这一点。这通常意味着有人很生气,他们的问题被否决了,并把它发泄在其他人身上。这条线有点奇怪。我个人认为,这是一个好答案,我投了两张反对票。虽然所有的答案都被否决了,但不是问题。有时候,我只是不了解这里的社区。你和我,都是。我特别不喜欢没有解释的否决票,但我最讨厌的是“开车经过”“我也是”的否决票,一旦你投了反对票,他们就会毫无解释地堆积在否决票上。顺便说一句,我正在努力应用你的答案。到目前为止,它看起来不错,这是可行的——但我忘了提到,这些组嵌套在不同文档中的不同深度。因此,我问了一个新问题来解决这个问题,并添加了我自己的暴力解决方案-feel-fre来改进它。我可以使用通配符来构建字典吗?我发现组名为“XXX错误定义”、“YYY错误定义”等。顺便说一句,我使用的是
xml.etree.ElementTree
,因此调用
findall()
会变得更糟。错误组似乎嵌套在不同文档的不同深度。给定错误的标题,例如“unknownDevice”,我如何在根目录下搜索所有内容以获得相应的
id
描述值?
我已更新了问题以反映我的评论。
import xml.etree.ElementTree as ET
dict01={}
tree=ET.parse('grpError.xml')
root=tree.getroot()
print (root)
docExe=root.findall('errordef') #Element reference
dict01=docExe[0].attrib #Store Attributes in dictionary
print (dict01)
print (dict01['id']) #Attributes of an element
print (dict01['name']) #Attributes of an element
print (docExe[0].find('description').text) #Child Elements inside parent Element
<Element 'group' at 0x000001A582EDB4A8>
{'id': '0x11', 'name': 'UnknownDevice'}
0x11
UnknownDevice
Indicated device is unknown