1

我有这个 xml,当我尝试获取节点时: InternetGatewayDevice->WANDevice->i2 with getElementsByTagName 它失败了。它仅适用于 xpath。这是代码:

XML:

<?xml version="1.0" encoding="UTF-8"?>
<InternetGatewayDevice>
        <LANDevice>
                <i1>
                </i1>
        </LANDevice>
        <Time>
                <X_ZyXEL_DefaultRouteProfileName PARAMETER="configured" TYPE="string" LENGTH="256"></X_ZyXEL_DefaultRouteProfileName>
                <LocalTimeZoneName PARAMETER="configured" TYPE="string" LENGTH="64">CET-1CEST-2,M3.5.0/1,M10.5.0/1</LocalTimeZoneName>
                <NTPServer5 PARAMETER="configured" TYPE="string" LENGTH="64"></NTPServer5>
                <NTPServer4 PARAMETER="configured" TYPE="string" LENGTH="64"></NTPServer4>
                <NTPServer3 PARAMETER="configured" TYPE="string" LENGTH="64"></NTPServer3>
                <NTPServer2 PARAMETER="configured" TYPE="string" LENGTH="64"></NTPServer2>
                <NTPServer1 PARAMETER="configured" TYPE="string" LENGTH="64">europe.pool.ntp.org</NTPServer1>
                <Enable PARAMETER="configured" TYPE="boolean">1</Enable>
        </Time>
        <WANDevice>
                <i1>
                        <WANEthernetInterfaceConfig>
                                <DuplexMode PARAMETER="configured" TYPE="string" LENGTH="32">Auto</DuplexMode>
                                <MaxBitRate PARAMETER="configured" TYPE="string" LENGTH="32">Auto</MaxBitRate>
                                <Enable PARAMETER="configured" TYPE="boolean">1</Enable>
                        </WANEthernetInterfaceConfig>
                        <WANConnectionDevice>
                                <i1>
                                        <X_ZyXEL_WANService PARAMETER="configured" TYPE="string" LENGTH="63"></X_ZyXEL_WANService>
                                        <X_ZyXEL_EgressQueue PARAMETER="configured" TYPE="uint8" MAX="255" MIN="0">0</X_ZyXEL_EgressQueue>
                                        <X_ZyXEL_L2VlanMuxMode PARAMETER="configured" TYPE="boolean">1</X_ZyXEL_L2VlanMuxMode>
                                        <WANIPConnection>
                                                <i1>
                                                        <X_ZyXEL_IPv6DefaultGateway PARAMETER="configured" TYPE="string" LENGTH="46">fe80::1</X_ZyXEL_IPv6DefaultGateway>
                                                        <RouteProtocolRx PARAMETER="configured" TYPE="string" LENGTH="10">Off</RouteProtocolRx>
                                                        <ConnectionTrigger PARAMETER="configured" TYPE="string" LENGTH="10">AlwaysOn</ConnectionTrigger>
                                                        <X_ZyXEL_Multicast PARAMETER="configured" TYPE="string" LENGTH="8">IGMPv3</X_ZyXEL_Multicast>
                                                        <DNSServers PARAMETER="configured" TYPE="string" LENGTH="64"></DNSServers>
                                                        <DefaultGateway PARAMETER="configured" TYPE="string" LENGTH="16"></DefaultGateway>
                                                        <SubnetMask PARAMETER="configured" TYPE="string" LENGTH="16"></SubnetMask>
                                                        <ExternalIPAddress PARAMETER="configured" TYPE="string" LENGTH="16"></ExternalIPAddress>
                                                        <AddressingType PARAMETER="configured" TYPE="string" LENGTH="8">DHCP</AddressingType>
                                                        <Name PARAMETER="configured" TYPE="string" LENGTH="256">EtherWAN1</Name>
                                                        <ConnectionType PARAMETER="configured" TYPE="string" LENGTH="13">IP_Routed</ConnectionType>
                                                        <X_ZyXEL_DualStack PARAMETER="configured" TYPE="uint8" MAX="2" MIN="0">1</X_ZyXEL_DualStack>
                                                        <X_ZyXEL_PassThrough PARAMETER="configured" TYPE="boolean">0</X_ZyXEL_PassThrough>
                                                        <X_ZyXEL_Routing PARAMETER="configured" TYPE="uint8" MAX="2" MIN="0">1</X_ZyXEL_Routing>
                                                        <X_ZyXEL_VlanMux8021p PARAMETER="configured" TYPE="uint8" MAX="7" MIN="0">0</X_ZyXEL_VlanMux8021p>
                                                        <MACAddressOverride PARAMETER="configured" TYPE="boolean">0</MACAddressOverride>
                                                        <MACAddress PARAMETER="configured" TYPE="mac">00:00:00:00:00:00</MACAddress>
                                                        <X_ZyXEL_AutoBridge PARAMETER="configured" TYPE="boolean">0</X_ZyXEL_AutoBridge>
                                                        <DNSOverrideAllowed PARAMETER="configured" TYPE="boolean">0</DNSOverrideAllowed>
                                                        <DNSEnabled PARAMETER="configured" TYPE="boolean">1</DNSEnabled>
                                                        <NATEnabled PARAMETER="configured" TYPE="boolean">1</NATEnabled>
                                                        <X_ZyXEL_L2Bridging_AvailableInterfaceKey PARAMETER="configured" TYPE="uint8" MAX="255" MIN="0">17</X_ZyXEL_L2Bridging_AvailableInterfaceKey>
                                                        <Enable PARAMETER="configured" TYPE="boolean">1</Enable>
                                                        <X_ZyXEL_VLanMuxId PARAMETER="configured" TYPE="uint16" MAX="4094" MIN="0">3900</X_ZyXEL_VLanMuxId>
                                                        <MaxMTUSize PARAMETER="configured" TYPE="uint16" MAX="1540" MIN="1">1500</MaxMTUSize>
                                                        <ShapingRate PARAMETER="configured" TYPE="sint31" MAX="2147483647" MIN="-1">-1</ShapingRate>
                                                        <X_ZyXEL_IPv6DNS>
                                                                <i1>
                                                                        <ServerIP PARAMETER="configured" TYPE="string" LENGTH="46">::1</ServerIP>
                                                                        <ServerType PARAMETER="configured" TYPE="string" LENGTH="32">FromISP</ServerType>
                                                                </i1>
                                                        </X_ZyXEL_IPv6DNS>
                                                        <X_ZyXEL_NAT>
                                                                <DefaultServerIP PARAMETER="configured" TYPE="string" LENGTH="16"></DefaultServerIP>
                                                                <DefaultServerIPEnabled PARAMETER="configured" TYPE="boolean">0</DefaultServerIPEnabled>
                                                                <Loopback PARAMETER="configured" TYPE="boolean">0</Loopback>
                                                                <LookupRule>
                                                                        <i1>
                                                                                <GlobalEnd PARAMETER="configured" TYPE="string" LENGTH="16">0.0.0.0</GlobalEnd>
                                                                                <GlobalStart PARAMETER="configured" TYPE="string" LENGTH="16">0.0.0.0</GlobalStart>
                                                                                <LocalEnd PARAMETER="configured" TYPE="string" LENGTH="16">255.255.255.255</LocalEnd>
                                                                                <LocalStart PARAMETER="configured" TYPE="string" LENGTH="16">0.0.0.0</LocalStart>
                                                                                <DeviceWANIP PARAMETER="configured" TYPE="boolean">1</DeviceWANIP>
                                                                                <NATRuleEnabled PARAMETER="configured" TYPE="boolean">1</NATRuleEnabled>
                                                                                <Type PARAMETER="configured" TYPE="uint8" MAX="3" MIN="0">1</Type>
                                                                        </i1>
                                                                </LookupRule>
                                                        </X_ZyXEL_NAT>
                                                        <X_ZyXEL_IPv6GlobalAddress>
                                                                <i1>
                                                                        <Address PARAMETER="configured" TYPE="string" LENGTH="46">::</Address>
                                                                        <AddressingType PARAMETER="configured" TYPE="string" LENGTH="10">DHCPv6</AddressingType>
                                                                        <PrefixLength PARAMETER="configured" TYPE="uint8" MAX="64" MIN="16">64</PrefixLength>
                                                                </i1>
                                                        </X_ZyXEL_IPv6GlobalAddress>
                                                        <X_ZyXEL_DNS>
                                                                <i1>
                                                                        <ServerIP PARAMETER="configured" TYPE="string" LENGTH="16">0.0.0.0</ServerIP>
                                                                        <ServerType PARAMETER="configured" TYPE="string" LENGTH="32">FromISP</ServerType>
                                                                </i1>
                                                        </X_ZyXEL_DNS>
                                                        <X_ZyXEL_DHCPv6Client>
                                                                <SentDHCPOption>
                                                                        <i1>
                                                                                <Value PARAMETER="configured" TYPE="string" LENGTH="340"></Value>
                                                                                <Tag PARAMETER="configured" TYPE="uint8" MAX="254" MIN="1">25</Tag>
                                                                                <Enable PARAMETER="configured" TYPE="boolean">1</Enable>
                                                                        </i1>
                                                                        <i2>
                                                                                <Value PARAMETER="configured" TYPE="string" LENGTH="340"></Value>
                                                                                <Tag PARAMETER="configured" TYPE="uint8" MAX="254" MIN="1">3</Tag>
                                                                                <Enable PARAMETER="configured" TYPE="boolean">1</Enable>
                                                                        </i2>
                                                                        <i3>
                                                                                <Value PARAMETER="configured" TYPE="string" LENGTH="340"></Value>
                                                                                <Tag PARAMETER="configured" TYPE="uint8" MAX="254" MIN="1">23</Tag>
                                                                                <Enable PARAMETER="configured" TYPE="boolean">1</Enable>
                                                                        </i3>
                                                                </SentDHCPOption>
                                                        </X_ZyXEL_DHCPv6Client>
                                                        <DHCPClient>
                                                                <SentDHCPOption>
                                                                        <i1>
                                                                                <Value PARAMETER="configured" TYPE="string" LENGTH="340"></Value>
                                                                                <Tag PARAMETER="configured" TYPE="uint8" MAX="254" MIN="1">60</Tag>
                                                                                <Enable PARAMETER="configured" TYPE="boolean">0</Enable>
                                                                        </i1>
                                                                </SentDHCPOption>
                                                        </DHCPClient>
                                                </i1>
                                        </WANIPConnection>
                                </i1>
                        </WANConnectionDevice>
                </i1>
                <i2>
                        <WANDSLInterfaceConfig>
                                <LinkEncapsulationRequested PARAMETER="configured" TYPE="string" LENGTH="64">G.992.3_Annex_K_ATM</LinkEncapsulationRequested>
                                <Status PARAMETER="configured" TYPE="string" LENGTH="64">Up</Status>
                                <Enable PARAMETER="configured" TYPE="boolean">1</Enable>
                        </WANDSLInterfaceConfig>
                        <WANConnectionDevice>
                                <i1>
                                        <X_ZyXEL_WANService PARAMETER="configured" TYPE="string" LENGTH="63"></X_ZyXEL_WANService>
                                        <X_ZyXEL_EgressQueue PARAMETER="configured" TYPE="uint8" MAX="255" MIN="0">0</X_ZyXEL_EgressQueue>
                                        <X_ZyXEL_L2VlanMuxMode PARAMETER="configured" TYPE="boolean">0</X_ZyXEL_L2VlanMuxMode>
                                        <WANPPPConnection>
                                                <i1>
                                                        <X_ZyXEL_PPPAuthenticationProtocol PARAMETER="configured" TYPE="string" LENGTH="32">Auto</X_ZyXEL_PPPAuthenticationProtocol>
                                                        <RouteProtocolRx PARAMETER="configured" TYPE="string" LENGTH="10">Off</RouteProtocolRx>
                                                        <ConnectionTrigger PARAMETER="configured" TYPE="string" LENGTH="10">AlwaysOn</ConnectionTrigger>
                                                        <PPPoEServiceName PARAMETER="configured" TYPE="string" LENGTH="256"></PPPoEServiceName>
                                                        <PPPoEACName PARAMETER="configured" TYPE="string" LENGTH="256"></PPPoEACName>
                                                        <X_ZyXEL_Multicast PARAMETER="configured" TYPE="string" LENGTH="8">IGMPv3</X_ZyXEL_Multicast>
                                                        <DNSServers PARAMETER="configured" TYPE="string" LENGTH="64"></DNSServers>
                                                        <Password PARAMETER="configured" TYPE="string" LENGTH="64">a</Password>
                                                        <Username PARAMETER="configured" TYPE="string" LENGTH="64">ads</Username>
                                                        <Name PARAMETER="configured" TYPE="string" LENGTH="256">AdslWAN1</Name>
                                                        <ConnectionType PARAMETER="configured" TYPE="string" LENGTH="16">IP_Routed</ConnectionType>
                                                        <X_ZyXEL_StaticIP PARAMETER="configured" TYPE="string" LENGTH="16">1xx.20.7.5</X_ZyXEL_StaticIP>
                                                        <X_ZyXEL_DualStack PARAMETER="configured" TYPE="uint8" MAX="2" MIN="0">0</X_ZyXEL_DualStack>
                                                        <X_ZyXEL_Routing PARAMETER="configured" TYPE="uint8" MAX="2" MIN="0">1</X_ZyXEL_Routing>
                                                        <X_ZyXEL_VlanMux8021p PARAMETER="configured" TYPE="uint8" MAX="7" MIN="0">0</X_ZyXEL_VlanMux8021p>
                                                        <X_ZyXEL_PassThrough PARAMETER="configured" TYPE="boolean">1</X_ZyXEL_PassThrough>
                                                        <MACAddressOverride PARAMETER="configured" TYPE="boolean">0</MACAddressOverride>
                                                        <MACAddress PARAMETER="configured" TYPE="mac">xx</MACAddress>
                                                        <X_ZyXEL_AutoBridge PARAMETER="configured" TYPE="boolean">0</X_ZyXEL_AutoBridge>
                                                        <DNSOverrideAllowed PARAMETER="configured" TYPE="boolean">0</DNSOverrideAllowed>
                                                        <DNSEnabled PARAMETER="configured" TYPE="boolean">1</DNSEnabled>
                                                        <NATEnabled PARAMETER="configured" TYPE="boolean">0</NATEnabled>
                                                        <X_ZyXEL_L2Bridging_AvailableInterfaceKey PARAMETER="configured" TYPE="uint8" MAX="255" MIN="0">0</X_ZyXEL_L2Bridging_AvailableInterfaceKey>
                                                        <Enable PARAMETER="configured" TYPE="boolean">1</Enable>
                                                        <X_ZyXEL_VLanMuxId PARAMETER="configured" TYPE="uint16" MAX="4094" MIN="0">0</X_ZyXEL_VLanMuxId>
                                                        <ShapingRate PARAMETER="configured" TYPE="sint31" MAX="2147483647" MIN="-1">-1</ShapingRate>
                                                        <IdleDisconnectTime PARAMETER="configured" TYPE="uint32" MAX="4294967295" MIN="0">0</IdleDisconnectTime>
                                                        <AutoDisconnectTime PARAMETER="configured" TYPE="uint32" MAX="4294967295" MIN="0">0</AutoDisconnectTime>
                                                        <X_ZyXEL_DNS>
                                                                <i1>
                                                                        <ServerIP PARAMETER="configured" TYPE="string" LENGTH="16">xx</ServerIP>
                                                                        <ServerType PARAMETER="configured" TYPE="string" LENGTH="32">UserDefined</ServerType>
                                                                </i1>
                                                                <i2>
                                                                        <ServerIP PARAMETER="configured" TYPE="string" LENGTH="16">xx</ServerIP>
                                                                        <ServerType PARAMETER="configured" TYPE="string" LENGTH="32">UserDefined</ServerType>
                                                                </i2>
                                                        </X_ZyXEL_DNS>
                                                        <X_ZyXEL_IPv6GlobalAddress>
                                                                <i1>
                                                                        <Address PARAMETER="configured" TYPE="string" LENGTH="46">::</Address>
                                                                        <AddressingType PARAMETER="configured" TYPE="string" LENGTH="10">DHCPv6</AddressingType>
                                                                        <PrefixLength PARAMETER="configured" TYPE="uint8" MAX="64" MIN="16">64</PrefixLength>
                                                                </i1>
                                                        </X_ZyXEL_IPv6GlobalAddress>
                                                        <X_ZyXEL_IPv6DNS>
                                                                <i1>
                                                                        <ServerIP PARAMETER="configured" TYPE="string" LENGTH="46">::</ServerIP>
                                                                        <ServerType PARAMETER="configured" TYPE="string" LENGTH="32">FromISP</ServerType>
                                                                </i1>
                                                        </X_ZyXEL_IPv6DNS>
                                                        <X_ZyXEL_DHCPv6Client>
                                                                <SentDHCPOption>
                                                                        <i1>
                                                                                <Value PARAMETER="configured" TYPE="string" LENGTH="340"></Value>
                                                                                <Tag PARAMETER="configured" TYPE="uint8" MAX="254" MIN="1">25</Tag>
                                                                                <Enable PARAMETER="configured" TYPE="boolean">1</Enable>
                                                                        </i1>
                                                                        <i2>
                                                                                <Value PARAMETER="configured" TYPE="string" LENGTH="340"></Value>
                                                                                <Tag PARAMETER="configured" TYPE="uint8" MAX="254" MIN="1">3</Tag>
                                                                                <Enable PARAMETER="configured" TYPE="boolean">1</Enable>
                                                                        </i2>
                                                                        <i3>
                                                                                <Value PARAMETER="configured" TYPE="string" LENGTH="340"></Value>
                                                                                <Tag PARAMETER="configured" TYPE="uint8" MAX="254" MIN="1">23</Tag>
                                                                                <Enable PARAMETER="configured" TYPE="boolean">1</Enable>
                                                                        </i3>
                                                                </SentDHCPOption>
                                                        </X_ZyXEL_DHCPv6Client>
                                                </i1>
                                        </WANPPPConnection>
                                        <WANDSLLinkConfig>
                                                <X_ZyXEL_IfName PARAMETER="configured" TYPE="string" LENGTH="16">nas1</X_ZyXEL_IfName>
                                                <ATMQoS PARAMETER="configured" TYPE="string" LENGTH="128">UBR</ATMQoS>
                                                <ATMEncapsulation PARAMETER="configured" TYPE="string" LENGTH="64">LLC</ATMEncapsulation>
                                                <DestinationAddress PARAMETER="configured" TYPE="string" LENGTH="256">PVC:8/35</DestinationAddress>
                                                <LinkType PARAMETER="configured" TYPE="string" LENGTH="32">EoA</LinkType>
                                                <X_ZyXEL_ATMEnbQos PARAMETER="configured" TYPE="boolean">0</X_ZyXEL_ATMEnbQos>
                                                <Enable PARAMETER="configured" TYPE="boolean">1</Enable>
                                                <X_ZyXEL_ATMSendBuffer PARAMETER="configured" TYPE="uint16" MAX="26144" MIN="0">0</X_ZyXEL_ATMSendBuffer>
                                                <ATMSustainableCellRate PARAMETER="configured" TYPE="uint32" MAX="4294967295" MIN="0">0</ATMSustainableCellRate>
                                                <ATMMaximumBurstSize PARAMETER="configured" TYPE="uint32" MAX="4294967295" MIN="0">0</ATMMaximumBurstSize>
                                                <ATMPeakCellRate PARAMETER="configured" TYPE="uint32" MAX="4294967295" MIN="0">0</ATMPeakCellRate>
                                        </WANDSLLinkConfig>
                                </i1>
                        </WANConnectionDevice>
                        <WANDSLDiagnostics>
                                <LoopDiagnosticsState PARAMETER="configured" EXTATTR="0x0002" TYPE="string" LENGTH="32">Complete</LoopDiagnosticsState>
                        </WANDSLDiagnostics>
                </i2>
        </WANDevice>
</InternetGatewayDevice>

php:

$xml = new DOMDocument('1.0', 'utf-8');
$xml->formatOutput = true;
$xml->preserveWhiteSpace = false;
$xml->load('file.xml');

//With getElementsByTagName
echo "getElementsByTagName:<br/>";
$node_of_xml = $xml->getElementsByTagName('InternetGatewayDevice')->item(0)->getElementsByTagName('WANDevice')->item(0)->getElementsByTagName('i2')->item(0);
foreach($node_of_xml->childNodes as $child) {
    echo $child->nodeName ."<br/>";
}
//With xpath
echo "<br/>Xpath:<br/>";
$xpath = new DOMXPath($xml);    
$results = $xpath->query('/InternetGatewayDevice/WANDevice/i2');   
$node_of_xml = $results->item(0);
foreach($node_of_xml->childNodes as $child) {
    echo $child->nodeName ."<br/>";
}

输出(getElementsByTagName 错误,Xpath 正确):

getElementsByTagName:
Value
Tag
Enable

Xpath:
WANDSLInterfaceConfig
WANConnectionDevice
WANDSLDiagnostics

PHP 版本 5.3.20 启用 DOM/XML DOM/XML API 版本 20031129 libxml 版本 2.7.8

4

1 回答 1

0

getElementsByTagName不限于直系子女。其中的第一个i2节点WANDevice/InternetGatewayDevice/WANDevice/i1/WANConnectionDevice/i1/WANIPConnection/i1/X_ZyXEL_DHCPv6Client/SentDHCPOption/i2

您自己提供了解决方案:XPath。

于 2013-01-25T09:54:26.483 回答