XML 中的 if-else 使用 ElementTree 查找解析

if-else in XML parsing with ElementTree find

我有一个很大的输入文件。我只是在下面提到一个示例:

      <metadata-record class='column'>
        <remote-name>Customer Name</remote-name>
        <remote-type>130</remote-type>
        <local-name>[Customer Name]</local-name>
        <parent-name>[Custom SQL Query]</parent-name>
        <remote-alias>Customer Name</remote-alias>
        <ordinal>5</ordinal>
        <local-type>string</local-type>
        <aggregation>Count</aggregation>
        <contains-null>true</contains-null>
        **<collation>LEN_RUS_S2_WO</collation>**
        <attributes>
          <attribute datatype='string' name='DebugRemoteType'>&quot;WSTR&quot;</attribute>
        </attributes>
      </metadata-record>

      <metadata-record class='column'>
        <remote-name>Discount</remote-name>
        <remote-type>5</remote-type>
        <local-name>[Discount]</local-name>
        <parent-name>[Custom SQL Query]</parent-name>
        <remote-alias>Discount</remote-alias>
        <ordinal>6</ordinal>
        <local-type>real</local-type>
        <aggregation>Sum</aggregation>
        **<precision>15</precision>**
        <contains-null>true</contains-null>
        <attributes>
          <attribute datatype='string' name='DebugRemoteType'>&quot;R8&quot;</attribute>
        </attributes>
      </metadata-record>

我想写 collat​​ion 可用时,否则 precision 标记值。但是当我尝试 运行 这段代码时,我无法获得后者(即使我确定相应的节点存在)。

这是我的代码:

import xml.etree.cElementTree as et
import shutil, os
colName  = []
colType  = []
colLname = []
colPname = []
colAlias = []
colOrd   = []
colLtype = []
colagg   = []
colcolla = []
colprec  = []
colcnull = []

xmlTree = et.parse('Input.xml')
xmlRoot = xmlTree.getroot()
for x in xmlRoot.findall('./connection/metadata-records/metadata-record'):
            colName.append(x.find('remote-name').text) 
            colType.append(x.find('remote-type').text) 
            colLname.append(x.find('local-name').text) 
            colPname.append(x.find('parent-name').text) 
            colAlias.append(x.find('remote-alias').text) 
            colOrd.append(x.find('ordinal').text) 
            colLtype.append(x.find('local-type').text) 
            colagg.append(x.find('aggregation').text)
            colcnull.append(x.find('contains-null').text)
            
            if x.find('precision'): 
                colprec.append(x.find('precision').text) 
                print("1")
            else:
                colcolla.append(x.find('collation').text) 
                print("2")

但我得到以下输出,Attrubute error:

2
---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
<ipython-input-10-2006350475d2> in <module>
     14                 print("1")
     15             else:
---> 16                 colcolla.append(x.find('collation').text)
     17                 print("2")
     18 

AttributeError: 'NoneType' object has no attribute 'text'

我的输出与输入相同,因为第一个元数据记录 'collation' 标签在那里,所以它应该被打印,因为下一个标签精度在那里,所以应该被打印。我期望的输出与上面粘贴的示例 XML 相同。现在我只获得带有排序规则标签的元数据记录,我没有得到精确度。

find方法ElementTreeclass

Returns an element instance or None.

您正在测试的条件 if x.find('precision'): 如果其参数等于布尔值 True,则评估为真。但是即使找到 precision 节点,returned 值也不会是布尔值!

您只需检查 find 的 return 值是否不同于 None:

if x.find('precision') is not None: 
    colprec.append(x.find('precision').text) 
    print("1")
else:
    colcolla.append(x.find('collation').text) 
    print("2")

输出:

2
1