如何使用名称空间和特殊标签解析xml

时间:2018-09-24 15:18:08

标签: php xml soap

我正在尝试解析xml字符串,但似乎无法正确地将xml文件正确设置为

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<GetBookableApartmentsResponse xmlns="http://tempuri.org/">
<GetBookableApartmentsResult>
<xs:schema id="BookableApartments" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
   <xs:element name="BookableApartments" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
      <xs:complexType>
         <xs:choice minOccurs="0" maxOccurs="unbounded">
            <xs:element name="Apartment">
               <xs:complexType>
                  <xs:sequence>
                     <xs:element name="Name" type="xs:string" minOccurs="0"/>
                     <xs:element name="BookingUrl" type="xs:string" minOccurs="0"/>
                     <xs:element name="BookingUrlMobile" type="xs:string" minOccurs="0"/>
                  </xs:sequence>
               </xs:complexType>
            </xs:element>
         </xs:choice>
      </xs:complexType>
   </xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
   <BookableApartments xmlns="">
      <Apartment diffgr:id="Apartment1" msdata:rowOrder="0">
         <Name>10419-0001</Name>
         <BookingUrl>http:/test.com</BookingUrl>
         <BookingUrlMobile></BookingUrlMobile>
      </Apartment>
      <Apartment diffgr:id="Apartment2" msdata:rowOrder="1">
         <Name>10419-0002</Name>
         <BookingUrl>http:test.com</BookingUrl>
         <BookingUrlMobile></BookingUrlMobile>
      </Apartment>

我想从BookableApartments的所有子项中获取数据,或者我想获取Apartment的数据。

我设置了我认为正确的名称空间:

$simpleXml = new \SimpleXmlElement($response);

$simpleXml->registerXPathNamespace('prefix', 'http://tempuri.org/');

$res = $simpleXml->xpath('//prefix:GetBookableApartmentsResponse');

$res = $res[0]->children('urn:schemas-microsoft-com:xml-diffgram-v1');

var_dump($res)exit;

但是我一直都得到结果:SimpleXMLElement {#957}

如果有人可以向我解释如何访问这些数据,那么访问这些数据需要遵循什么逻辑?

我知道我必须设置名称空间,但是从xml中可以看出,我还有一个特殊标记:<diffgr:diffgram,我们将不胜感激。

0 个答案:

没有答案