简体   繁体   English

使用PHP在DOM上查找(XML)节点

[英]Find (XML) Node on DOM with PHP

I'm having trouble finding some node on WSDL (XML) document with DOMDocument PHP. 我无法使用DOMDocument PHP在WSDL(XML)文档上找到某些节点。 When I use getElementsByTagName it always return NULL. 当我使用getElementsByTagName时,它总是返回NULL。 what I'm about to find is xsd:complexType but when I try different node, let say types, it's return a object. 我要查找的是xsd:complexType,但是当我尝试其他节点时,可以说类型,它返回一个对象。

This is my WSDL (XML) Document 这是我的WSDL(XML)文档

<definitions xmlns:SOAP-ENV="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:SOAP-ENC="http://schemas.xmlsoap.org/soap/encoding/" xmlns:tns="urn:routeDx2" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns="http://schemas.xmlsoap.org/wsdl/" targetNamespace="urn:routeDx2">
<types>
<xsd:schema targetNamespace="urn:routeDx2"
>
 <xsd:import namespace="http://schemas.xmlsoap.org/soap/encoding/" />
 <xsd:import namespace="http://schemas.xmlsoap.org/wsdl/" />
 <xsd:complexType name="inputCashin">
  <xsd:all>
   <xsd:element name="userName" type="xsd:string"/>
   <xsd:element name="signature" type="xsd:string"/>
   <xsd:element name="productCode" type="xsd:string"/>
   <xsd:element name="merchantCode" type="xsd:string"/>
   <xsd:element name="terminal" type="xsd:string"/>
   <xsd:element name="merchantNumber" type="xsd:string"/>
   <xsd:element name="transactionType" type="xsd:string"/>
   <xsd:element name="recipientNumber" type="xsd:string"/>
   <xsd:element name="recipientName" type="xsd:string"/>
   <xsd:element name="amount" type="xsd:string"/>
   <xsd:element name="feeAmount" type="xsd:string"/>
   <xsd:element name="traxId" type="xsd:string"/>
   <xsd:element name="timeStamp" type="xsd:string"/>
  </xsd:all>
 </xsd:complexType>
 <xsd:complexType name="inputCashout">
  <xsd:all>
   <xsd:element name="userName" type="xsd:string"/>
   <xsd:element name="signature" type="xsd:string"/>
   <xsd:element name="productCode" type="xsd:string"/>
   <xsd:element name="merchantCode" type="xsd:string"/>
   <xsd:element name="terminal" type="xsd:string"/>
   <xsd:element name="merchantNumber" type="xsd:string"/>
   <xsd:element name="transactionType" type="xsd:string"/>
   <xsd:element name="recipientNumber" type="xsd:string"/>
   <xsd:element name="recipientName" type="xsd:string"/>
   <xsd:element name="amount" type="xsd:string"/>
   <xsd:element name="feeAmount" type="xsd:string"/>
   <xsd:element name="verifyingCode" type="xsd:string"/>
   <xsd:element name="traxId" type="xsd:string"/>
   <xsd:element name="timeStamp" type="xsd:string"/>
  </xsd:all>
 </xsd:complexType>
</xsd:schema>
</types>
<definitions>

what I'm about to find is xsd:complexType or if that's possible, how to find by attribute name that has unique value (verifyingCode or else), how do I accomplish this via PHP and DOMDocument? 我要查找的是xsd:complexType,或者,如果可能的话,如何通过具有唯一值(verifyingCode或其他值)的属性名称来查找,如何通过PHP和DOMDocument实现呢?

Besides your incorrect closing tag for <definitions> (last line), I don't see a problem. 除了<definitions>不正确的结束标记(最后一行)之外,我没有看到任何问题。

$doc = new DOMDocument();
$doc->loadXML($xml);
$complexTypes = $doc->getElementsByTagName('complexType');
printf('<p>Found %d complexType elements</p>', $complexTypes->length);
foreach ($complexTypes as $complexType) {
    echo $complexType->getAttribute('name'), '<br>';
}

Demo - http://codepad.viper-7.com/95XDsS 演示-http: //codepad.viper-7.com/95XDsS

You're probably including the xsd namespace in your tag name query. 您可能在标记名称查询中包含了xsd命名空间。 If you read the docs , you will see the argument to getElementsByTagName() specifies 如果您阅读文档 ,则将看到getElementsByTagName()的参数指定

The local name (without namespace) of the tag to match on 要匹配的标签的本地名称(无名称空间)

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

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