Python3實現(xiàn)解析XML文件并存入Excel表中
更新時間:2025年12月18日 15:27:51 作者:Asia-Lee
這篇文章主要為大家詳細介紹了如何使用Python3實現(xiàn)解析XML文件并存入Excel表中,文中的示例代碼講解詳細,感興趣的小伙伴可以了解一下
1. XML文件部分數(shù)據(jù)
<?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="2017-05-12" xsi:schemaLocation="http://nvd.nist.gov/feeds/cve/1.2 https://scap.nist.gov/schema/nvd/nvd-cve-feed_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="CONFIRM" url="http://www.openbsd.org/errata23.html#tcpfix">http://www.openbsd.org/errata23.html#tcpfix</ref>
</refs>
<vuln_soft>
<prod name="freebsd" vendor="freebsd">
<vers num="1.1.5.1"/>
<vers num="2.2.8"/>
<vers num="2.2"/>
<vers num="1.0"/>
<vers num="2.1.7"/>
<vers num="2.2.6"/>
<vers num="2.1.6.1"/>
<vers num="3.0"/>
<vers num="2.0"/>
<vers num="2.1.7.1"/>
<vers num="1.1"/>
<vers num="2.2.2"/>
<vers num="1.2"/>
<vers num="2.0.5"/>
<vers num="2.2.3"/>
<vers num="2.0.1"/>
<vers num="2.1.5"/>
<vers num="2.2.4"/>
<vers num="2.1.6"/>
<vers num="2.2.5"/>
</prod>
<prod name="bsd_os" vendor="bsdi">
<vers num="3.1"/>
</prod>
<prod name="openbsd" vendor="openbsd">
<vers num="2.4"/>
<vers num="2.3"/>
</prod>
</vuln_soft>
</entry>2. Python3解析XML并將數(shù)據(jù)寫入Excel表中
# -*- coding: utf-8 -*-
import xml.dom.minidom #導(dǎo)入處理xml文件的模塊
import pandas as pd
#打開xml文檔 并將這個文件對象存入dom變量
dom = xml.dom.minidom.parse('dataset_source/NVD/nvdcve-2002.xml')
#得到文檔元素對象
root = dom.documentElement #用于得到dom對象的文檔元素,并把獲得的對象給root
#獲得標(biāo)簽為entry的多組標(biāo)簽
entry_tag=dom.getElementsByTagName('entry')
data1=entry_tag[0] #表示多組標(biāo)簽中的第一個,entry_tag[2]表示這多組標(biāo)簽中的第三個
cve_name=data1.getAttribute("name") #獲得元素屬性對應(yīng)的值
print(cve_name)
lenth=entry_tag.length #獲取xml文件中標(biāo)簽對為entry的個數(shù)
print(len(entry_tag)) #獲取xml文件中標(biāo)簽對為entry的個數(shù)
print(lenth)
descript_tag=dom.getElementsByTagName('descript') #獲得標(biāo)簽為entry的多組標(biāo)簽
data2=descript_tag[0]
descript=data2.firstChild.data #獲得標(biāo)簽對之間的數(shù)據(jù)
print(descript)
#獲取entry標(biāo)簽的子標(biāo)簽descript之間的數(shù)據(jù)
data3=dom.getElementsByTagName('entry')[0].getElementsByTagName('descript')[0].firstChild.data
print(data3)
cve_list=[]
descript_list=[]
cve_list.append(cve_name) #將獲得的數(shù)據(jù)存入列表
descript_list.append(descript)
all_dict={'CVE':cve_list,'Decript':descript_list} #將列表存儲為字典
df = pd.DataFrame(all_dict) #將字典轉(zhuǎn)換為DataFrame
#將DataFrame數(shù)據(jù)寫入excel表中
with pd.ExcelWriter('new.xls') as Writer:
df.to_excel(Writer,'Sheet1',index=False)結(jié)果為:

到此這篇關(guān)于Python3實現(xiàn)解析XML文件并存入Excel表中的文章就介紹到這了,更多相關(guān)Python3解析XML內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Python光學(xué)仿真學(xué)習(xí)衍射算法初步理解
這篇文章主要為大家介紹了Python光學(xué)仿真學(xué)習(xí)中對衍射算法的初步理解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步2021-10-10
python使用post提交數(shù)據(jù)到遠程url的方法
這篇文章主要介紹了python使用post提交數(shù)據(jù)到遠程url的方法,涉及Python使用post傳遞數(shù)據(jù)的相關(guān)技巧,需要的朋友可以參考下2015-04-04
django rest framework之請求與響應(yīng)(詳解)
下面小編就為大家?guī)硪黄猟jango rest framework之請求與響應(yīng)(詳解)。小編覺得挺不錯的,現(xiàn)在就分享給大家,也給大家做個參考。一起跟隨小編過來看看吧,希望對大家有所幫助2017-11-11
Python+requests+unittest執(zhí)行接口自動化測試詳情
這篇文章主要介紹了Python+requests+unittest執(zhí)行接口自動化測試詳情,文章圍繞主題展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的朋友可以參考一下2022-09-09

