0

I'm working parsing an XML file, can anyone help me to parse the last and innermost child node.

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

can anyone help to parse this innermost child node in 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>

Especially this command cmd user command???

  • What have you tried so far? Which data are you finding it difficult to process? – Nigel Ren Jun 06 '18 at 06:01
  • There's libraries existing for that purpose. If you really want to do it yourself, use recursivity – Cid Jun 06 '18 at 06:45
  • i need to parse the enable secret password username user secret password no username user especially i need the output, but im unable to parse it , i get data till 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: after that its not coming??? can please provide any code –  Jun 06 '18 at 06:48
  • https://stackoverflow.com/questions/3577641/how-do-you-parse-and-process-html-xml-in-php?rq=1 – ThW Jun 06 '18 at 08:50
  • I tired recursive method, but I want to parse out the data from this , please provide a sudo code if possible ?? –  Jun 06 '18 at 09:12
  • Can somebody post a pseudo code for this parsing –  Jun 06 '18 at 19:13

1 Answers1

0

Depending on which part of the data you want, you can load it into SimpleXML and then use XPath to search for the <code> element, so a basic version would be...

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

would give you (with the test data)...

command=enable secret
cmduser=password
cmduser=user

Or if you want to pick the <command> element with both the user and password, you can use XPath to select the element with 2 <cmduser> elements...

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

Which give

cmduser1=user
cmduser2=password
Nigel Ren
  • 56,122
  • 11
  • 43
  • 55