2016-04-02 33 views
2

Ben Bu benim ayrıştırmak çalışıyorum XML dosyasıdır bir subchildelementtree bulmak() her zaman döndürür Yok

içeriğini bulmak için bir XML dosyası ayrıştırmak için Python ile elementtree kullanıyorum:

<?xml version='1.0' encoding='UTF-8'?> 
<nvd xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://nvd.nist.gov/feeds/cve/1.2" nvd_xml_version="1.2" pub_date="2016-02-10" xsi:schemaLocation="http://nvd.nist.gov/feeds/cve/1.2 http://nvd.nist.gov/schema/nvdcve_1.2.1.xsd"> 
    <entry type="CVE" name="CVE-1999-0001" seq="1999-0001" published="1999-12-30" modified="2010-12-16" severity="Medium" CVSS_version="2.0" CVSS_score="5.0" CVSS_base_score="5.0" CVSS_impact_subscore="2.9" CVSS_exploit_subscore="10.0" CVSS_vector="(AV:N/AC:L/Au:N/C:N/I:N/A:P)"> 
    <desc> 
     <descript source="cve">ip_input.c in BSD-derived TCP/IP implementations allows remote attackers to cause a denial of service (crash or hang) via crafted packets.</descript> 
    </desc> 
    <loss_types> 
     <avail/> 
    </loss_types> 
    <range> 
     <network/> 
    </range> 
    <refs> 
     <ref source="OSVDB" url="http://www.osvdb.org/5707">5707</ref> 
     <ref source="CONFIRM" url="http://www.openbsd.org/errata23.html#tcpfix">http://www.openbsd.org/errata23.html#tcpfix</ref> 
    </refs> 

bu benim kodudur:

import xml.etree.ElementTree as ET 

if __name__ == '__main__': 
    tree = ET.parse('nvdcve-modified.xml') 
    root = tree.getroot() 

    print root.find('entry') 
    print root[0].find('desc') 

Yok hem hatları için çıkış

cevap

2

XML'niz varsayılan ad alanı d vardır Kök öğesi düzeyinde efined: öneki olmadan

xmlns="http://nvd.nist.gov/feeds/cve/1.2" 

Alçalan elemanları örtülü atalarının varsayılan ad alanını devralır. ad alanında eleman bulmak için, ad URI bir önek harita ve şöyle öneki kullanabilirsiniz:

ns = {'d': 'http://nvd.nist.gov/feeds/cve/1.2'} 
root.find('d:entry', ns) 

veya doğrudan ad URI kullanın: Bu işleri

root.find('{http://nvd.nist.gov/feeds/cve/1.2}entry') 
+0

Teşekkür! –

İlgili konular