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条)

报告相同问题?

悬赏问题

  • ¥20 搭建pt1000三线制高精度测温电路
  • ¥15 使用Jdk8自带的算法,和Jdk11自带的加密结果会一样吗,不一样的话有什么解决方案,Jdk不能升级的情况
  • ¥15 画两个图 python或R
  • ¥15 在线请求openmv与pixhawk 实现实时目标跟踪的具体通讯方法
  • ¥15 八路抢答器设计出现故障
  • ¥15 opencv 无法读取视频
  • ¥15 按键修改电子时钟,C51单片机
  • ¥60 Java中实现如何实现张量类,并用于图像处理(不运用其他科学计算库和图像处理库))
  • ¥20 5037端口被adb自己占了
  • ¥15 python:excel数据写入多个对应word文档