【问题标题】:XML parsing for inner most child node in phpphp中最内部子节点的XML解析
【发布时间】:2018-06-06 05:46:58
【问题描述】:

我正在解析一个 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 中的这个最里面的子节点?

 <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>

尤其是这个命令cmd用户命令???

【问题讨论】:

  • 到目前为止你尝试过什么?您发现哪些数据难以处理?
  • 存在为此目的的库。如果您真的想自己做,请使用递归
  • 我需要解析 <command>enable secret <cmduser>password</cmduser></command><command>username <cmduser>user</cmduser> secret <cmduser>password </cmduser></command><command>no username <cmduser>user</cmduser></command> 特别是我需要输出,但我无法解析它,我获取数据直到 Cisco Catalyst Switch 设备上可以使用以下命令来设置启用密码、使用密码创建本地用户并删除本地用户:之后它不会出现???可以请提供任何代码
  • 我厌倦了递归方法,但我想从这个 <cmduser><command></command></cmduser> 中解析出数据,如果可能,请提供一个 sudo 代码 ??

标签: php arrays xml


【解决方案1】:

根据您想要的数据部分,您可以将其加载到 SimpleXML 中,然后使用 XPath 搜索 &lt;code&gt; 元素,因此基本版本将是...

$fileName = "out.xml";
$xml = simplexml_load_file($fileName);

$code = $xml->xpath("//code");
echo "command=".trim($code[0]->command).PHP_EOL;
echo "cmduser=".trim($code[0]->command->cmduser).PHP_EOL;
echo "cmduser=".trim($code[0]->command[1]->cmduser[0]).PHP_EOL;

会给你(带有测试数据)...

command=enable secret
cmduser=password
cmduser=user

或者,如果您想选择包含用户名和密码的 &lt;command&gt; 元素,您可以使用 XPath 选择包含 2 个 &lt;cmduser&gt; 元素的元素...

$code = $xml->xpath("//code/command[count(cmduser)=2]");
echo "cmduser1=".trim($code[0]->cmduser[0]).PHP_EOL;
echo "cmduser2=".trim($code[0]->cmduser[1]).PHP_EOL;

哪个给

cmduser1=user
cmduser2=password

【讨论】:

猜你喜欢
  • 2015-03-22
  • 1970-01-01
  • 2023-04-06
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-12-28
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多