2015-09-28 83 views
0

我有一个带有3个命名空间的XML。使用python 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> 
[] 

我能够得到根标签,但不能访问内部命名空间的标签。

你能帮我解决问题吗?我如何读取所有内部命名空间标签中的数据?

回答

0

这是因为你试图得到的目标元素不是直接的根元素的孩子。您需要或者指定到目标元件从根全路径:

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) 
+0

谢谢,这个解决方案工作。 :) –