正在读取子元素的属性


Reading the attribute of a child element

我正在尝试读取以下xml中location的值:

<service name="xyz">
     <documentation>gSOAP 2.7.11 generated service definition</documentation>
     <port name="xyz" binding="tns:xyz">
      <SOAP:address location="http://192.168.0.222:8092"/>
     </port>
</service>

我正在尝试访问SOAP:address标签,但无法:

$wsdlFile = file_get_contents('./wyz.wsdl');
    if($wsdlFile) {
        $xml = simplexml_load_string($wsdlFile);
        foreach( $xml->service->documentation->port->attributes() as $a => $b) {
            echo $a . '-' . $b;
        }
    }

如何获取location的值?

//Convert to Array like this
$wsdlFile = file_get_contents('./wyz.wsdl');
if($wsdlFile) {
    $wsdlData = json_decode(json_encode($wsdlFile),TRUE);
}
//Now you can access the address through the key=> value pair
$location = $wsdlData['location'];

在属性中包含:会使获取该属性变得更加困难,但这是可能的:

按索引:

$value = $xml->port->children()[0]->attributes()['location']->__toString();

或按名称:

$prop = 'SOAP:address';
$value = $xml->port->$prop->attributes()['location']->__toString();

打印SOAP:address的所有属性及其值

$wsdlFile = file_get_contents('./wyz.wsdl');
if($wsdlFile) {
    $xml = simplexml_load_string($wsdlFile);
    $prop = 'SOAP:address';
    foreach($xml->port->$prop->attributes() as $a => $b) {
        echo $a . '-' . $b;
    }
}