2015-12-21 145 views
1

我需要從xml中獲取「joinmeetingurl」元素的值。我試圖以下面的方式。但它什麼都沒有返回。請幫我取值。從XML對象提取值?

<?php 

$xml = '<serv:message xmlns:serv="http://www.webex.com/schemas/2002/06/service" 
     xmlns:com="http://www.webex.com/schemas/2002/06/common" 
     xmlns:meet="http://www.webex.com/schemas/2002/06/service/meeting" 
     xmlns:att="http://www.webex.com/schemas/2002/06/service/attendee"> 
      <serv:header> 
      <serv:response> 
       <serv:result>SUCCESS</serv:result> 
       <serv:gsbstatus>PRIMARY</serv:gsbstatus> 
      </serv:response> 
      </serv:header> 
      <serv:body> 
      <serv:bodycontent xsi:type="meet:getjoinurlMeetingResponse" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
       <meet:joinmeetingurl>meetingURL</meet:joinmeetingurl> 
      </serv:bodycontent> 
     </serv:body> 
     </serv:message>'; 

    $xml = simplexml_load_string($xml); 
    $items = $xml->registerXPathNamespace('meet','http://www.webex.com/schemas/2002/06/service/meeting'); 

    $resp = $xml->xpath('//meet:joinmeetingurl'); 

?> 

我總是獲得$ resp的空值。

回答

1

你的XPath應該已經工作很簡單,你可以投的元素string獲得的價值,例如:

$xml = <<<XML 
<serv:message xmlns:serv="http://www.webex.com/schemas/2002/06/service" 
     xmlns:com="http://www.webex.com/schemas/2002/06/common" 
     xmlns:meet="http://www.webex.com/schemas/2002/06/service/meeting" 
     xmlns:att="http://www.webex.com/schemas/2002/06/service/attendee"> 
      <serv:header> 
      <serv:response> 
       <serv:result>SUCCESS</serv:result> 
       <serv:gsbstatus>PRIMARY</serv:gsbstatus> 
      </serv:response> 
      </serv:header> 
      <serv:body> 
      <serv:bodycontent xsi:type="meet:getjoinurlMeetingResponse" 
     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"> 
       <meet:joinmeetingurl>meetingURL</meet:joinmeetingurl> 
      </serv:bodycontent> 
     </serv:body> 
     </serv:message> 
XML; 

$xml = simplexml_load_string($xml); 
$xml->registerXPathNamespace('meet','http://www.webex.com/schemas/2002/06/service/meeting'); 
$resp = $xml->xpath('//meet:joinmeetingurl'); 
echo (string)$resp[0]; 

eval.in demo

輸出:

meetingURL 
0

可提供與simplexml和相關功能沒有指導,但它似乎與標準DOMDocumentDOMXPath

$dom=new DOMDocument; 
$dom->loadXML($xml); 

$xpath=new DOMXPath($dom); 
$col=$xpath->query('//meet:joinmeetingurl'); 
foreach($col as $node)echo $node->nodeValue; 
$dom=null;