2015-09-28 82 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

謝謝,這個解決方案工作。 :) –