首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >处理XML文档

处理XML文档
EN

Stack Overflow用户
提问于 2014-03-25 14:36:23
回答 1查看 136关注 0票数 1

我有以下XML文档,并试图构建一个过滤器,从元素中返回元素的内容(如果存在的话)。

到目前为止,我有以下代码:

代码语言:javascript
运行
复制
val xml = XML.loadString(output)
val ports = xml \\ "ports"

然后,我认为我需要这样做来构建过滤器,但我不能让它像我所期望的那样工作。

代码语言:javascript
运行
复制
for (port <- ports \\ "port"){
    println(port \\ "port" filter{ _ \\"script" exists } \\ "script")
  }

代码语言:javascript
运行
复制
<?xml version="1.0"?>
<?xml-stylesheet href="file:///usr/local/share/nmap/nmap.xsl" type="text/xsl"?>
<!-- Nmap 6.40 scan initiated Tue Mar 25 14:14:09 2014 as: /usr/local/bin/nmap -&#45;datadir /usr/local/share/nmap -&#45;script=ssl-cert -p 443,80 -v -oX - www.adobe.com -->
<nmaprun scanner="nmap"
         args="/usr/local/bin/nmap -&#45;datadir /usr/local/share/nmap -&#45;script=ssl-cert -p 443,80 -v -oX - www.abc.com"
         start="1395756849" startstr="Tue Mar 25 14:14:09 2014" version="6.40" xmloutputversion="1.04">
    <scaninfo type="connect" protocol="tcp" numservices="2" services="80,443"/>
    <verbose level="1"/>
    <debugging level="0"/>
    <taskbegin task="Ping Scan" time="1395756849"/>
    <taskend task="Ping Scan" time="1395756849" extrainfo="1 total hosts"/>
    <taskbegin task="Parallel DNS resolution of 1 host." time="1395756849"/>
    <taskend task="Parallel DNS resolution of 1 host." time="1395756849"/>
    <taskbegin task="Connect Scan" time="1395756849"/>
    <taskend task="Connect Scan" time="1395756849" extrainfo="2 total ports"/>
    <taskbegin task="NSE" time="1395756849"/>
    <taskend task="NSE" time="1395756849"/>
    <host starttime="1395756849" endtime="1395756849">
        <status state="up" reason="syn-ack" reason_ttl="0"/>
        <address addr="111.111.111.11" addrtype="ipv4"/>
        <hostnames>
            <hostname name="www.abc.com" type="user"/>
            <hostname name="www.abc.com" type="PTR"/>
        </hostnames>
        <ports>
            <port protocol="tcp" portid="80">
                <state state="open" reason="syn-ack" reason_ttl="0"/>
                <service name="http" method="table" conf="3"/>
            </port>
            <port protocol="tcp" portid="443">
                <state state="open" reason="syn-ack" reason_ttl="0"/>
                <service name="https" method="table" conf="3"/>
                <script id="ssl-cert" output="placeholder text">
                    <table key="subject">
                        <elem key="commonName">www.abc.com</elem>
                        <elem key="organizationalUnitName">Info</elem>
                        <elem key="stateOrProvinceName">London</elem>
                        <elem key="countryName">UK</elem>
                        <elem key="organizationName">ABC Incorporated</elem>
                        <elem key="localityName">London</elem>
                    </table>
                    <table key="issuer">
                        <elem key="commonName">VeriSign Class 3 International Server CA - G3</elem>
                        <elem key="countryName">US</elem>
                        <elem key="organizationalUnitName">Terms of use at https://www.verisign.com/rpa (c)10</elem>
                        <elem key="organizationName">VeriSign, Inc.</elem>
                    </table>
                    <table key="pubkey">
                        <elem key="type">rsa</elem>
                        <elem key="bits">2048</elem>
                    </table>
                    <table key="validity">
                        <elem key="notBefore">2012-11-04T00:00:00+00:00</elem>
                        <elem key="notAfter">2014-11-23T23:59:59+00:00</elem>
                    </table>
                    <elem key="md5">0b96b21786ab67c5531cff08cf044c6b</elem>
                    <elem key="sha1">b3cd7fec0ade71acb0fe83be5e723495313ff426</elem>
                    <elem key="pem">-placeholder</elem>
                </script>
            </port>
        </ports>
        <times srtt="106198" rttvar="99430" to="503918"/>
    </host>
    <runstats>
        <finished time="1395756849" timestr="Tue Mar 25 14:14:09 2014" elapsed="0.70"
                  summary="Nmap done at Tue Mar 25 14:14:09 2014; 1 IP address (1 host up) scanned in 0.70 seconds"
                  exit="success"/>
        <hosts up="1" down="0" total="1"/>
    </runstats>
</nmaprun>
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2014-03-25 15:56:23

script的过滤并不是真正必要的,因为\\将遍历整个结构并返回那些匹配的元素。

代码语言:javascript
运行
复制
xml \\ "script"

res1: scala.xml.NodeSeq = NodeSeq(<script id="ssl-cert" output="placeholder text">
...

script中打印res1标记

代码语言:javascript
运行
复制
res1.foreach(println(_)) 

<script id="ssl-cert" output="placeholder text">
<table key="subject">
...

关于注释的更新

只获取包含port节点的script节点

代码语言:javascript
运行
复制
(xml \\ "ports" \\ "port").filter(n => n.descendant.exists(_.label == "script"))

产出:

代码语言:javascript
运行
复制
res81: scala.xml.NodeSeq =
NodeSeq(<port protocol="tcp" portid="443">
                <state state="open" reason="syn-ack" reason_ttl="0"/>
                <service name="https" method="table" conf="3"/>
                <script>
                    <table key="subject">
                        ...

descendant给出了一个List[Node],这是一种方法。还有其他的,但这对我有用。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/22637757

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档