Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/292.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用php获取xml元素数据_Php_Xml - Fatal编程技术网

使用php获取xml元素数据

使用php获取xml元素数据,php,xml,Php,Xml,我使用curl获取xml对象 但我似乎无法输入xml元素数据 我尝试使用: $sxe = new SimpleXMLElement($result); $final_xml = $sxe->asXML(); echo $final_xml -> Answer -> Status; <pre><?php $entries = simplexml_load_file("test.xml"); $namespaces = $entries->getNa

我使用curl获取xml对象

但我似乎无法输入xml元素数据

我尝试使用:

$sxe = new SimpleXMLElement($result);
$final_xml = $sxe->asXML();
echo  $final_xml ->  Answer -> Status;
<pre><?php

$entries = simplexml_load_file("test.xml");
$namespaces = $entries->getNamespaces(true);

var_dump($namespaces);

$ns3s = $entries->children($namespaces['S'])
            ->Body
            ->children($namespaces['ns7'])
            ->GetStopMonitoringServiceResponse
            ->children()
            ->Answer
            ->children($namespaces['ns3']);

var_dump($ns3s);
有了那个例子,我什么也得不到

在这个例子中,我得到了一个长字符串形式的xml数组,数组中的每个位置在xml字符串中都是不同的注释

这是我得到的xml输出,我想从中检索数据: 如何从这个xml中检索数据或将其更改为JSON,然后检索数据

塔克斯


2014-12-04T11:11:55.585+02:00
ISR Siri服务器(141.10)
89061165
000234:1351677777:4684
真的
2014-12-04T11:11:55.585+02:00
0
真的
2014-12-04T11:11:00.000+02:00
1448001046
40262
4687
3.
12
3.
40110
2014-12-04T10:45:00.000+02:00
34.94065475463867
32.428466796875
40262
2014-12-04T11:12:00.000+02:00
尝试以下测试代码:

getnamespace(true);
var_dump($名称空间);
$ns3s=$entries->children($namespace['S'])
->身体
->子项($namespace['ns7']))
->GetStopMonitoringServiceResponse
->儿童()
->答复
->儿童($namespace['ns3']);
var_dump(3美元);

看这里:嗨,我试过了,但仍然没有发现我做错了什么?我尝试了语法echo$result->children('S',true)->Envelope->children()->Body->children('ns7',true)->GetStopMonitoringServiceResponse->children()->Answer->children('ns3',true)->Status;塔克斯!这解决了我的问题。我使用了“simplexml\u load\u string”而不是“simplexml\u load\u file”,这很有效
<?xml version="1.0" encoding="UTF-8"?>
  <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Body>
    <ns7:GetStopMonitoringServiceResponse xmlns:ns3="http://www.siri.org.uk/siri" xmlns:ns4="http://www.ifopt.org.uk/acsb" xmlns:ns5="http://www.ifopt.org.uk/ifopt" xmlns:ns6="http://datex2.eu/schema/1_0/1_0" xmlns:ns7="http://new.webservice.namespace">
   <Answer>
      <ns3:ResponseTimestamp>2014-12-04T11:11:55.585+02:00</ns3:ResponseTimestamp>
      <ns3:ProducerRef>ISR Siri Server (141.10)</ns3:ProducerRef>
      <ns3:ResponseMessageIdentifier>89061165</ns3:ResponseMessageIdentifier>
      <ns3:RequestMessageRef>000234:1351677777:4684</ns3:RequestMessageRef>
      <ns3:Status>true</ns3:Status>
      <ns3:StopMonitoringDelivery version="IL2.7">
          <ns3:ResponseTimestamp>2014-12-04T11:11:55.585+02:00</ns3:ResponseTimestamp>
          <ns3:RequestMessageRef>0</ns3:RequestMessageRef>
          <ns3:Status>true</ns3:Status>
          <ns3:MonitoredStopVisit>
               <ns3:RecordedAtTime>2014-12-04T11:11:00.000+02:00</ns3:RecordedAtTime>
               <ns3:ItemIdentifier>1448001046</ns3:ItemIdentifier>
               <ns3:MonitoringRef>40262</ns3:MonitoringRef>
               <ns3:MonitoredVehicleJourney>
                    <ns3:LineRef>4687</ns3:LineRef>
                    <ns3:DirectionRef>3</ns3:DirectionRef>
                    <ns3:PublishedLineName>12</ns3:PublishedLineName>
                    <ns3:OperatorRef>3</ns3:OperatorRef>
                    <ns3:DestinationRef>40110</ns3:DestinationRef>
                    <ns3:OriginAimedDepartureTime>2014-12-04T10:45:00.000+02:00</ns3:OriginAimedDepartureTime>
                    <ns3:VehicleLocation>
                         <ns3:Longitude>34.94065475463867</ns3:Longitude>
                         <ns3:Latitude>32.428466796875</ns3:Latitude>
                    </ns3:VehicleLocation>
                    <ns3:MonitoredCall>
                         <ns3:StopPointRef>40262</ns3:StopPointRef>
                         <ns3:ExpectedArrivalTime>2014-12-04T11:12:00.000+02:00</ns3:ExpectedArrivalTime>
                    </ns3:MonitoredCall>
               </ns3:MonitoredVehicleJourney>
          </ns3:MonitoredStopVisit>
      </ns3:StopMonitoringDelivery>
    </Answer>
</ns7:GetStopMonitoringServiceResponse>
</S:Body>
</S:Envelope>
<pre><?php

$entries = simplexml_load_file("test.xml");
$namespaces = $entries->getNamespaces(true);

var_dump($namespaces);

$ns3s = $entries->children($namespaces['S'])
            ->Body
            ->children($namespaces['ns7'])
            ->GetStopMonitoringServiceResponse
            ->children()
            ->Answer
            ->children($namespaces['ns3']);

var_dump($ns3s);