0
<section index="2.3" title="No HTTP(S) Server Session Timeout or HTTP(S) Server Idle Timeout Set" ref="ADMINISTRATION.WEBTIMEOUT.NO.HTTP.OR.HTTPS.SESSION.OR.IDLE.TIMEOUT.four">
    <issuedetails>
        <devices>
            <device name="Switch" type="Cisco Catalyst Switch" osversion="16.3" />
        </devices>
        <ratings type="Nipperv1">
            <rating>High</rating>
            <impact>Critical</impact>
            <ease>Easy</ease>
            <fix>Quick</fix>
            <FindingID>NSA-ADMIN-046</FindingID>
            <classif>Administration</classif>
        </ratings>
    </issuedetails>
    <section index="2.3.1" title="Finding" ref="FINDING">
    </section>
    <section index="2.3.4" title="Recommendation" ref="RECOMMENDATION">
        <text>Nipper Studio recommends that a HTTP(S) server session timeout period of 10 minutes or less should be configured.</text>
        <text>Notes for Cisco Catalyst Switch devices:</text>
        <text>The HTTP server timeout can be configured with the following command:<code><command>ip http timeout-policy idle <cmduser>seconds</cmduser> life <cmduser>seconds</cmduser> requests <cmduser>number</cmduser></command>
        </code>
        </text>

XML解析器:

$commands = $section->xpath('section[4]/text/code/command');
$object->commands = "";
foreach($commands as $command)
{
    $object->commands .=  $command;
    $cmdusers = $command->xpath('cmduser');
    foreach($cmdusers as $cmduser){
        $object->commands .=  $cmduser;
    }
    $object->commands .=  "<br>";
}
echo "commands : <br>".$object->commands;
echo "<be>";

输出:

ip http timeout-policy idle seconds life seconds requests number

但它是这样来的

ip http timeout-policy idle life requests secondssecondsnumber
4

2 回答 2

0

在这种情况下,您可以只使用一个 XPath:

$commands = $section->xpath('section[4]/text/code/command//text()');
于 2021-06-21T18:47:08.593 回答
0

您已经看到您的期望与实际结果不符。

这只是 SimpleXML 的一个限制,因此它有点属于“错误的工作工具”类别。但请继续阅读,它是多才多艺的。

是什么让您感到困难,因为您使用 SimpleXML 解析器的 XML 结构不太适合 - 至少在这个地方。

有问题的部分是寻找跨越多个元素节点的文本数据。

正如在cOle2的回答中使用嵌套标签回显 xml 文件的内容所述:

当前,您的 $output 是一个 SimpleXMLElement,当您回显它时,会调用内部 toString() 方法。根据注释,此方法不返回此元素子元素内的文本内容,这就是排除某些文本的原因。

在使用 SimpleXML 时获取文本内容的一种方法是使用 DOM 姐妹库,这与我对另一个问题的回答类似,它是一个简单的导入操作,然后获取属性:

$commands = $section->xpath('section[4]/text/code/command');
$object->commands = "";
foreach($commands as $command)
{
    $object->commands .=  dom_import_simplexml($command)->textContent;
                                  ^^^^            ^^^         ^^^^
    ...

根据经验,SimpleXML 对 XML 文本节点很弱。它的 API 中没有任何具体的内容,字符串值表示叶子元素节点的文本内容。其他元素节点字符串内容可能会出现不完整和错误排序 - 就像您可以体验到的一样。

于 2021-07-10T19:33:30.637 回答