简体   繁体   English

如何在PHP中使用XPath解析此XML

[英]How to parse this XML using XPath in Php

I'm trying to parse the XML with XPath, but I'm unable to get data out, kindly help me with a Pseudocode, Need to print the data using XPath in PHP, I'm unable to understand how to print each tag. 我正在尝试使用XPath解析XML,但无法获取数据,请使用伪代码帮助我,需要在PHP中使用XPath打印数据,我无法理解如何打印每个标签。 kindly need some advice regarding this XPath in PHP. 请在PHP中与此XPath相关的一些建议。

        <?xml version="1.0" encoding="utf-8"?>    
                     <Report>
                     <section index="2.2" title="Users With A Weak Authentication Password" ref="AUTHENTICATION.USERS.WEAKPASSWORD">
        <issuedetails>
          <devices>
            <device name="AD-FIN-2" type="Cisco Catalyst Switch" osversion="15.0" />
          </devices>
          <ratings type="Nipperv1">
            <rating>High</rating>
            <impact>Critical</impact>
            <ease>Moderate</ease>
            <fix>Quick</fix>
          </ratings>
        </issuedetails>
        <section index="2.2.1" title="Finding" ref="FINDING">
          <text>Access to restricted network user and administration services are typically secured using username and password authentication credentials. The strength of the authentication credentials is even more important if the service allows for devices to be reconfigured or it allows access to potentially sensitive information.</text>
          <text>DWS identified two user accounts with a weak password on AD-FIN-2. These are listed in Table <linktotable ref="AUTHENTICATION.USERS.WEAKPASSWORD.1">8</linktotable> and includes administrative access to the device.</text>
          <table index="8" title="Users on AD-FIN-2 with a weak password" ref="AUTHENTICATION.USERS.WEAKPASSWORD.1">
            <headings>
              <heading>User</heading>
              <heading>Password</heading>
              <heading>Privilege</heading>
              <heading>Weakness</heading>
            </headings>
            <tablebody>
              <tablerow>
                <tablecell><item>enable (password)</item></tablecell>
                <tablecell><item>R@j!magic</item></tablecell>
                <tablecell><item>15</item></tablecell>
                <tablecell><item>No numbers</item></tablecell>
              </tablerow>
              <tablerow>
                <tablecell><item>admin</item></tablecell>
                <tablecell><item>R@j!magic</item></tablecell>
                <tablecell><item>1</item></tablecell>
                <tablecell><item>No numbers</item></tablecell>
              </tablerow>
            </tablebody>
          </table>
        </section>
        <index="2.2.2" title="Impact" ref="IMPACT">
          <text>A malicious user, or remote attacker, who is able to connect to an administrative service will be able to authenticate to the device without using a password. The attacker will then be able to perform administrative and user level tasks. This could include re-configuring the device, extracting potentially sensitive information and disabling the device. Once an attacker has obtained the configuration from the device they may be able to identify authentication credentials that could then be used to gain access to other network devices.</text>
        </section>
        <section index="2.2.3" title="Ease" ref="EASE">
          <text>Password brute-forcing tools and techniques have been widely documented on the Internet and published media. Although there are a number of different tools available, brute-forcing authentication credentials can be problematic.</text>
          <list type="numbererd">
            <listitem>Account lockout facilities can quickly prevent access to the account.</listitem>
            <listitem>Device protection mechanisms may slow or disconnect connections where multiple authentication attempts are made in a short period of time.</listitem>
            <listitem>Brute-forcing can be very time consuming, especially if the password is long or made up of various character types.</listitem>
            <listitem>Network administrators may be alerted to locked out accounts or authentication attempts.</listitem>
          </list>
        </section>
        <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>
      </section>
          </Report>

MyCode: This is the code which I have used to parse the XML, but I'm unable to print data out or array? MyCode:这是我用来解析XML的代码,但是我无法打印数据或数组? kindly help me with a solution. 请为我提供解决方案。

$xmlContent = file_get_contents('toshibaconfig2.xml');
$dom = new DOMDocument("1.0", "UTF-8");
$dom->preserveWhiteSpace = false;

$dom->loadXml($xmlContent);

$xpath = new DOMXPath($dom);

//
// Add namespaces automatically
//
// Fetch the namespaces, add a few lines to register these back with the document
// so that you can use them in XPath expressions...
foreach ($xpath->query('namespace::*', $dom->documentElement) as $node) {
    //echo $node-> . '=' . $node->nodeValue, "\n";
    $xpath->registerNamespace($node->localName, $node->nodeValue);
}


// Root node
$rootNode = $xpath->query('/report');

// Finding data
$Intro = $xpath->query('section/issuedetails/text', $rootNode)->item(0)->nodeValue;
$table = $xpath->query('table/headings/tablebody', $rootNode)->item(0)->nodeValue;
$list = $xpath->query('text/list/listitem', $rootNode)->item(0)->nodeValue;

// code data
$itemList = array();
$codedata = $xpath->query('command/cmduser/cmduser', $rootNode);

// Then loop over the items...

/** @var DOMNode $invoiceItemNode */
foreach ($codedata as $codedt) {
    $row = [];

    /** @var DOMNode $field */
    foreach ($codedt->childNodes as $field) {
        $row[$field->tagName] = $field->nodeValue;
    }
    $itemList[] = $row;
}

print_r($itemList);

You've asked for /report but the root element of your XML is called <Report> . 您已要求/report但XML的根元素称为<Report> XML is case-sensitive. XML区分大小写。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM