简体   繁体   English

Python将XML输出与列表进行比较

[英]Python comparing XML output to a list

I have an XML that looks something like this: 我有一个看起来像这样的XML:

<Import>
  <spId>1234</spId>
  <GroupFlag>false</GroupFlag>
</Import>

I want to extract the value of spId and compare it with a list and I have the following script: 我想提取spId的值并将其与列表进行比较,我有以下脚本:

import xml.etree.ElementTree as ET
xml_file = "c:/somefile.xml"

sp_id_list = ['1234']
tree = ET.parse(xml_file)
root = tree.getroot()

for sp_id in root.findall('./spId'):
  if sp_id.text in sp_id_list:
    print sp_id.text

This doesn't work for spId (numeric) but works for comparing GroupFlag (string) with a list. 这不适用于spId(数字),但适用于将GroupFlag(字符串)与列表进行比较。 Why is this happening and how can I rectify this problem? 为什么会发生这种情况,如何解决这个问题?

Sorry for the stupid question, I am a noob to this. 很抱歉这个愚蠢的问题,我对此不满意。

Your code example works correctly if your XML sample posted here is given as input XML file. 如果此处发布的XML示例作为输入XML文件给出,则您的代码示例将正常工作。

However you want to find all elements. 但是,您想查找所有元素。 So, I assume that your real document has many <Import> items. 因此,我假设您的真实文档中有许多<Import>项目。 If a list of items is not wrapped by some parent tag it is not a valid XML. 如果项目列表没有被某些父标记包装,则它不是有效的XML。 In that case you would have xml.etree.ElementTree.ParseError . 在这种情况下,您将拥有xml.etree.ElementTree.ParseError

So, I assume that in your real document <Import> is not a root element and <Import> elements are somewhere deeper in the document, for example 因此,我假设在您的真实文档中, <Import>不是根元素,而<Import>元素在文档中更深处,例如

<Parent>
  <Import>
    <spId>1234</spId>
    <GroupFlag>false</GroupFlag>
  </Import>
  <Import>
    <spId>1234</spId>
    <GroupFlag>false</GroupFlag>
  </Import>
</Parent>

In that case the search pattern './spId' cannot find those tags, since that pattern matches only direct children of the root element. 在那种情况下,搜索模式'./spId'找不到那些标记,因为该模式仅匹配根元素的直接子代。 So, you can use XPath matching tags all levels beneath or even better pointing direct path from the root to the level where spId is located: 因此,您可以使用XPath匹配标签,在所有级别以下甚至更好地指向从根到spId所在级别的直接路径:

# all subelements, on all levels beneath the current element
root.findall('.//spId')

# all spId elements directly in Import tags that are directly
# beneath the root element (as in the above XML example)
root.findall('./Import/spId'):

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM