golang解析xml文件
通过etree解析xml文件思路:通过SelectElement()找到数据根节点和子节点通过SelectAttrValue()获取属性值实现:解析出示例中的 IP、type、协议、端口、时间
·
golang 解析xml文件
- xml示例:
<?xml version="1.0"?>
<nmaprun scanner="masscan" start="1658195179" version="1.0-BETA" xmloutputversion="1.03">
<scaninfo type="syn" protocol="tcp" />
<host endtime="1658195179">
<address addr="138.36.21.4" addrtype="ipv4"/>
<ports>
<port protocol="tcp" portid="50000">
<state state="open" reason="syn-ack" reason_ttl="42"/>
</port>
</ports>
</host>
<host endtime="1658195179">
<address addr="138.36.22.211" addrtype="ipv4"/>
<ports>
<port protocol="tcp" portid="50000">
<state state="open" reason="syn-ack" reason_ttl="41"/>
</port>
</ports>
</host>
<host endtime="1658195179">
<address addr="138.36.22.238" addrtype="ipv4"/>
<ports>
<port protocol="tcp" portid="50000">
<state state="open" reason="syn-ack" reason_ttl="41"/>
</port>
</ports>
</host>
<host endtime="1658195179">
<address addr="138.36.22.239" addrtype="ipv4"/>
<ports>
<port protocol="tcp" portid="50000">
<state state="open" reason="syn-ack" reason_ttl="41"/>
</port>
</ports>
</host>
<host endtime="1658195179">
<address addr="138.36.22.204" addrtype="ipv4"/>
<ports>
<port protocol="tcp" portid="50000">
<state state="open" reason="syn-ack" reason_ttl="42"/>
</port>
</ports>
</host>
<host endtime="1658195179">
<address addr="138.36.22.244" addrtype="ipv4"/>
<ports>
<port protocol="tcp" portid="50000">
<state state="open" reason="syn-ack" reason_ttl="41"/>
</port>
</ports>
</host>
<host endtime="1658195179">
<address addr="138.36.20.223" addrtype="ipv4"/>
<ports>
<port protocol="tcp" portid="50000">
<state state="open" reason="syn-ack" reason_ttl="40"/>
</port>
</ports>
</host>
<host endtime="1658195179">
<address addr="138.36.20.193" addrtype="ipv4"/>
<ports>
<port protocol="tcp" portid="50000">
<state state="open" reason="syn-ack" reason_ttl="42"/>
</port>
</ports>
</host>
<host endtime="1658195179">
<address addr="138.36.22.47" addrtype="ipv4"/>
<ports>
<port protocol="tcp" portid="50000">
<state state="open" reason="syn-ack" reason_ttl="42"/>
</port>
</ports>
</host>
<runstats>
<finished time="1658195190" timestr="2022-07-18 21:46:30" elapsed="11" />
<hosts up="9" down="0" total="9" />
</runstats>
</nmaprun>
-
通过etree解析xml文件
思路:
-
通过SelectElement()找到数据根节点和子节点
-
通过SelectAttrValue()获取属性值
-
-
实现:
解析出示例中的 IP、type、协议、端口、时间
import ( "github.com/beevik/etree" "log" ) // 使用etree解析masscan的xml文件 func ParseMasscanXml(filePath string) { doc := etree.NewDocument() if err := doc.ReadFromFile(filePath); err != nil { log.Printf("解析%v失败, Error------>%v", filePath, err) return } // 找到数据的根节点nmaprun,nmaprun的子节点host和finished中获取IP及时间等信息 if nmaprun := doc.SelectElement("nmaprun"); nmaprun != nil { // 找到finished节点,获取时间 finishedRoot := nmaprun.SelectElement("runstats").SelectElement("finished") timestr := finishedRoot.SelectAttrValue("timestr", "") // 获取IP及端口 for _, host := range nmaprun.SelectElements("host") { if address := host.SelectElement("address"); address != nil { ip := address.SelectAttrValue("addr", "") ipType := address.SelectAttrValue("addrtype", "") } for _, portRoot := range host.SelectElements("ports") { protocol := "" portId := "" if portInfo := portRoot.SelectElement("port"); portInfo != nil { protocol = portInfo.SelectAttrValue("protocol", "") portId = portInfo.SelectAttrValue("portid", "") } } } } }
魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)