首页 > 解决方案 > 如何使用Php中的Xpath解析XML中单个子节点中的子多个节点

问题描述

朋友,我无法从下面给出的 XML 中的子节点打印子子节点,请帮我解决这个问题。

XML:

<section index="2.2.4" title="Recommendation" ref="RECOMMENDATION">
          <text>DWS strongly recommends that all authentication credentials should be configured with a strong password.</text>
          <text>DWS recommends that:</text>
          <list type="bullet">
            <listitem>passwords should be at least eight characters in length;</listitem>
            <listitem>characters in the password should not be repeated more than five times;</listitem>
            <listitem>passwords should include both upper case and lower case characters;</listitem>
            <listitem>passwords should include numbers;</listitem>
            <listitem>passwords should include punctuation characters;</listitem>
            <listitem>passwords should not include the username;</listitem>
            <listitem>passwords should not include a device's name, make or model;</listitem>
            <listitem>passwords should not be based on dictionary words.</listitem>
          </list>
          <text>Notes for Cisco Catalyst Switch devices:</text>
          <text>The following commands can be used on Cisco Catalyst Switch devices to set the enable password, create a local user with a password and to delete a local user:<code><command>enable secret <cmduser>password</cmduser></command>
<command>username <cmduser>user</cmduser> secret <cmduser>password</cmduser></command>
<command>no username <cmduser>user</cmduser></command>
</code></text>
        </section>

代码:

<?php
$xml = simplexml_load_file("toshibaconfig2.xml");

$report = $xml->xpath("//section [@index = '2.2.4'] //code");

//foreach ($report as $reports)

print_r($report);

我需要按<code></code>以下顺序解析输出:

enable secret password
username user secret password
no username user

请帮我解决这个问题。

标签: phpxmlparsingxpath

解决方案


推荐阅读