Xpath grep child并替换为top元素

时间:2018-02-04 22:11:33

标签: java xpath

我正在尝试解决问题,将子节点提升一级,并删除带有子级的旧属性。

我的xml看起来像这样:

<results>    
<result id="6f6d6f1f-1c9f-4f3f-b77a-0430504c6626">
    <name>TCP timestamps</name>
    <host>192.168.0.1
        <asset asset_id="cb779852-fa5f-4097-ba57-2f202c04bf68"/>
    </host>
    <port>general/tcp</port>
    <nvt oid="1.3.6.1.4.1.25623.1.0.80091">
        <xref>URL:http://www.ietf.org/rfc/rfc1323.txt</xref>
        <tags>cvss_base_vector=AV:N/AC:H/Au:N/C:P/I:N/A:N|summary=The remote host implements TCP timestamps and
            therefore allows to compute
            the uptime.|vuldetect=Special IP packets are forged and sent with a little delay in between to the
            target IP. The responses are searched for a timestamps. If found, the timestamps are
            reported.|solution=To disable TCP timestamps on linux add the line 'net.ipv4.tcp_timestamps = 0' to
            /etc/sysctl.conf. Execute 'sysctl -p' to apply the settings at runtime.

            To disable TCP timestamps on Windows execute 'netsh int tcp set global timestamps=disabled'

            Starting with Windows Server 2008 and Vista, the timestamp can not be completely disabled.

            The default behavior of the TCP/IP stack on this Systems is to not use the
            Timestamp options when initiating TCP connections, but use them if the TCP peer
            that is initiating communication includes them in their synchronize (SYN) segment.

            See also: http://www.microsoft.com/en-us/download/details.aspx?id=9152|affected=TCP/IPv4 implementations
            that implement RFC1323.|insight=The remote host implements TCP timestamps, as defined by
            RFC1323.|impact=A side effect of this feature is that the uptime of the remote
            host can sometimes be computed.|solution_type=Mitigation|qod_type=remote_banner
        </tags>
        <cert/>
    </nvt>
    <threat>Low</threat>
    <severity>2.6</severity>
    <description>It was detected that the host implements RFC1323.

        The following timestamps were retrieved with a delay of 1 seconds in-between:
        Packet 1: 2961998095
        Packet 2: 1703213053
    </description>
    <overrides/>
</result>

所以我想删除但需要孩子。我该怎么做才能用标签替换nvt?必须与名称,主机,端口等处于同一级别。是否有像replace这样的函数?我怎么能解决这个问题,我之前尝试过:

 NodeList cleanNodeList0 = null;
    try {
        cleanNodeList0 = (NodeList) xpath.compile("//result/nvt/tags").evaluate(doc, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }


    NodeList cleanNodeList4 = null;
    try {
        cleanNodeList4 = (NodeList) xpath.compile("//result/nvt").evaluate(doc, XPathConstants.NODESET);
    } catch (XPathExpressionException e) {
        e.printStackTrace();
    }
    for (int i = cleanNodeList4.getLength() - 1; i >= 0; i--) {
        cleanNodeList4.item(i).getParentNode().removeChild(cleanNodeList4.item(i));
        cleanNodeList4.item(i).appendChild(cleanNodeList0.item(i));
    }

1 个答案:

答案 0 :(得分:0)

XPath永远无法修改您的XML结构:您通常会在XSLT中构建新树(如果您喜欢低级编码,则使用Java构建)。

我无法确定您(但可能)不符合它们的现有代码的具体要求。