duan2428 2019-05-12 12:29
浏览 60
已采纳

我试图获取值或所有a:属性[重复]

I have used PHP SimpleXML, simplexml_load_string, and i get no response even after using DOM and SimpleXML

//Php XML Response

$xml = <<<XML
    <s:Envelope xmlns:s="http://schemas.xmlsoap.org/soap/envelope/">
    <s:Body>
    <StatusResponse xmlns="http://tempuri.org/">
    <Result xmlns:a="http://schemas.datacontract.org/2004/07/OS.FLC" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <a:ResCode>OK</a:ResCode>
    <a:IDNumber>149897</a:IDNumber>
    <a:CusName>AXIOS ASOS</a:CusName>
    <a:HoldName>AXA AXIOS</a:HoldName>
    </Result>
    </StatusResponse>
    </s:Body>
    </s:Envelope>
    XML;

// using PHP SIMPLE XML

$domContent = new SimpleXMLElement(((string)$xml), LIBXML_COMPACT);
$test = $domContent->xpath('/Result/a/*');
foreach ($test as $node) 
{
     print_r($node);
}

//Using DOM

$domp = new DOMDocument('1.0', 'utf-8');
$domp->loadXML($xml);
$items = $domp->getElementsByTagName('a');
foreach ($items as $item) 
{
     $HOLDER = $item->getElementsByTagNameNS("http://schemas.datacontract.org/2004/07/OS.FLC", "*")->Result(0)->nodeValue;
     echo "HoldName = " . $HOLDER . PHP_EOL;
}


// Expected Result

ResCode = OK
IDNumber = 149897
CusName = AXIOS ASOS
</div>
  • 写回答

1条回答 默认 最新

  • duanbin198788 2019-05-12 14:44
    关注

    Using DOMDocument, you can use getElementsByTagNameNS and specify the whole URL of a :

    $doc = new DOMDocument('1.0', 'utf-8');
    $doc->loadXML($xml);
    
    $elements = $doc->getElementsByTagNameNS('http://schemas.datacontract.org/2004/07/OS.FLC', '*');
    foreach($elements as $elem)
    {
        echo $elem->nodeName . ' -> ' . $elem->nodeValue ;
    }
    

    Output :

    a:ResCode -> OK
    a:IDNumber -> 149897
    a:CusName -> AXIOS ASOS
    a:HoldName -> AXA AXIOS
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?