dousi9215 2019-04-24 16:09
浏览 64
已采纳

从xml标记中提取值

I´m trying to extract the RecordID = "1014276" from a tag

I tried with :

$result = curl_exec($ch);
curl_close($ch);

$xml2 = simplexml_load_string($result);
echo $latitude = (string) $xml2['RecordID'];

This is the XML response:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
   <soap:Body>
      <ns1:createDataResponse xmlns:ns1="http://3e.pl/ADInterface">
         <StandardResponse RecordID="1014276" xmlns="http://3e.pl/ADInterface"/>
      </ns1:createDataResponse>
   </soap:Body>
</soap:Envelope>
  • 写回答

2条回答 默认 最新

  • douhuang4166 2019-04-24 16:19
    关注

    This involves a bit more than just accessing the attribute, first you have to select the correct element. Using XPath is the most comment way in this sort of structure. As this has a default namespace defined for the data, you will need to register this with the SimpleXMLElement first (using $xml2->registerXPathNamespace("ns1","http://3e.pl/ADInterface");.

    You can then find the element using the XPAth expression //ns1:StandardResponse. As the xpath() method returns a list of found elements, use [0] to just extract the first match. You should then be able to extract the attribute as in your code using the resultant element...

    $xml2 = simplexml_load_string($result);
    $xml2->registerXPathNamespace("ns1","http://3e.pl/ADInterface");
    
    $response = $xml2->xpath("//ns1:StandardResponse")[0];
    echo (string) $response['RecordID'];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 fluent的在模拟压强时使用希望得到一些建议
  • ¥15 STM32驱动继电器
  • ¥15 Windows server update services
  • ¥15 关于#c语言#的问题:我现在在做一个墨水屏设计,2.9英寸的小屏怎么换4.2英寸大屏
  • ¥15 模糊pid与pid仿真结果几乎一样
  • ¥15 java的GUI的运用
  • ¥15 Web.config连不上数据库
  • ¥15 我想付费需要AKM公司DSP开发资料及相关开发。
  • ¥15 怎么配置广告联盟瀑布流
  • ¥15 Rstudio 保存代码闪退