使用 python 中的命名空间解析 XML 3 没有给出任何数据

parsing XML with namespace in python 3 gives no data

我有一个带有 3 个命名空间的 XML。

<?xml version="1.0" encoding="UTF-8"?>
<cus:Customizations xmlns:cus="http://www.bea.com/wli/config/customizations" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xt="http://www.bea.com/wli/config/xmltypes">
  <cus:customization xsi:type="cus:EnvValueCustomizationType">
    <cus:description/>
    <cus:envValueAssignments>
      <xt:envValueType>working manager</xt:envValueType>
      <xt:location xsi:nil="true"/>
      <xt:owner>
        <xt:type>FLOW</xt:type>
        <xt:path>/somedir/dir/somepath3</xt:path>
      </xt:owner>
      <xt:value xsi:type="xs:string" xmlns:xs="http://www.w3.org/2001/XMLSchema"/>
    </cus:envValueAssignments>
  </cus:customization>
  <cus:customization xsi:type="cus:FindAndReplaceCustomizationType">
    <cus:description/>
    <cus:query>
      <xt:resourceTypes>ProxyService</xt:resourceTypes>
      <xt:resourceTypes>SMTPServer</xt:resourceTypes>
          <xt:resourceTypes>SSconection</xt:resourceTypes>
      <xt:refsToSearch xsi:type="xt:ResourceRefType">
        <xt:type>FLOW</xt:type>
        <xt:path>/somedir/dir/somepath2</xt:path>
          </xt:refsToSearch>
      <xt:includeOnlyModifiedResources>false</xt:includeOnlyModifiedResources>
      <xt:searchString>Search String</xt:searchString>
      <xt:isCompleteMatch>false</xt:isCompleteMatch>
    </cus:query>
    <cus:replacement>Replacement String</cus:replacement>
  </cus:customization>
  <cus:customization xsi:type="cus:ReferenceCustomizationType">
    <cus:description/>
    <cus:refsToBeConsidered xsi:type="xt:ResourceRefType">
      <xt:type>FLOW</xt:type>
      <xt:path>/somedir/dir/somepath</xt:path>
    </cus:refsToBeConsidered>
        <cus:refsToBeConsidered xsi:type="xt:ResourceRefType">
      <xt:type>WSDL</xt:type>
      <xt:path>/somedir/dir/somepath</xt:path>
    </cus:refsToBeConsidered>
    <cus:refsToBeConsidered xsi:type="xt:ResourceRefType">
      <xt:type>ProxyService</xt:type>
      <xt:path>/somedir/dir/somepath</xt:path>
    </cus:refsToBeConsidered>
    <cus:externalReferenceMap>
      <xt:oldRef>
        <xt:type>FLOW</xt:type>
        <xt:path>/somedir/dir/somepath</xt:path>
      </xt:oldRef>
      <xt:newRef>
        <xt:type>FLOW</xt:type>
        <xt:path>/somedir/dir/somepath</xt:path>
      </xt:newRef>
        </cus:externalReferenceMap>
    <cus:externalReferenceMap>
      <xt:oldRef>
        <xt:type>XMLSchema</xt:type>
        <xt:path>/somedir/dir/somepath</xt:path>
      </xt:oldRef>
      <xt:newRef>
        <xt:type>XMLSchema</xt:type>
        <xt:path>/somedir/dir/somepath</xt:path>
      </xt:newRef>
    </cus:externalReferenceMap>
    <cus:externalReferenceMap>
      <xt:oldRef>
        <xt:type>XMLSchema</xt:type>
        <xt:path>/somedir/dir/somepath</xt:path>
      </xt:oldRef>
      <xt:newRef>
        <xt:type>XMLSchema</xt:type>
        <xt:path>/somedir/dir/somepath</xt:path>
      </xt:newRef>
    </cus:externalReferenceMap>
  </cus:customization>
</cus:Customizations>

我在 python 3 中使用 lxml,但我得到的是空数据。当我打印根时,它会给我根标签。 这是我的代码。

#!/usr/bin/python3

import sys
import os
import os.path
import csv
import xml.etree.ElementTree as etree
import lxml.etree

times = []
keys = []
tree2 = lxml.etree.parse('/home/vagrant/dev_dir/ALSBCustomizationFile.xml')
NSMAP = {'cus': 'http://www.bea.com/wli/config/customizations',
         'xsi': 'http://www.w3.org/2001/XMLSchema-instance',
         'xt': 'http://www.bea.com/wli/config/xmltypes'}

root22 = tree2.getroot()

print(root22)
namespace = root22.findall('cus:Customizations', NSMAP)
namespace2 = root22.findall('xsi:customization', NSMAP)
namespace3 = root22.findall('xt:envValueType', NSMAP)

print(namespace3)

当我 运行 这个脚本时,我得到以下输出。

<Element {http://www.bea.com/wli/config/customizations}Customizations at 0x7faadb3a0508>
[]

我能够获取根标签,但无法访问内部命名空间标签。

你能帮我看看我错在哪里吗?如何读取所有内部名称空间标记中的数据。?

那是因为您要获取的目标元素不是根元素的直接子元素。您需要指定从根目录到目标元素的完整路径:

namespace3 = root22.findall('cus:customization/cus:envValueAssignments/xt:envValueType', NSMAP)

或在 XPath 的开头使用相对后代或自身轴 (.//):

namespace3 = root22.findall('.//xt:envValueType', NSMAP)

为了稍后执行更复杂的 XPath 表达式,您最好使用 lxmlxpath() 方法,它提供更好的 XPath 支持:

namespace3 = root22.xpath('.//xt:envValueType', namespaces=NSMAP)