使用PHP中的命名空间解析XML回复

时间:2013-12-03 13:52:14

标签: php xml soap namespaces

我从SOAP Web服务获得以下XML回复:

<?xml version="1.0" encoding="UTF-8"?>
<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/">
   <soapenv:Body>
      <ns:executeResponse xmlns:ns="http://inforcyl/services/JourneySearch/1.2">
         <ns:return xmlns:ax21123="http://input.journeysearch.v_1_2.services.inforcyl.com/xsd" xmlns:ax21124="http://services.inforcyl.com/xsd" xmlns:ax21126="http://journeysearch_input.input.journeysearch.v_1_2.services.inforcyl.com/xsd" xmlns:ax21130="http://output.journeysearch.v_1_2.services.inforcyl.com/xsd" xmlns:ax21132="http://journeysearch_output.output.journeysearch.v_1_2.services.inforcyl.com/xsd" xmlns:ax21134="http://journey.journeysearch_output.output.journeysearch.v_1_2.services.inforcyl.com/xsd" xmlns:ax21137="http://leg.journey.journeysearch_output.output.journeysearch.v_1_2.services.inforcyl.com/xsd" type="com.inforcyl.services.v_1_2.journeysearch.output.JourneySearch_output">
            <ax21130:descInfo />
            <ax21130:errorMessage />
            <ax21130:header type="com.inforcyl.services.Header">
               <ax21124:authentification type="com.inforcyl.services.Authentification">
                  <ax21124:admon>999</ax21124:admon>
                  <ax21124:clientIp>127.0.0.1</ax21124:clientIp>
                  <ax21124:password xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true" />
                  <ax21124:user>internet</ax21124:user>
                  <ax21124:userDataBase>01</ax21124:userDataBase>
                  <ax21124:userLocaleCountry>GB</ax21124:userLocaleCountry>
                  <ax21124:userLocaleLanguage>en</ax21124:userLocaleLanguage>
                  <ax21124:userTimeUsing>+1</ax21124:userTimeUsing>
               </ax21124:authentification>
               <ax21124:maintenanceMessage />
               <ax21124:systemDate>20131203141055</ax21124:systemDate>
            </ax21130:header>
            <ax21130:info />
            <ax21130:journeys type="com.inforcyl.services.v_1_2.journeysearch.output.journeysearch_output.Journey">
               <ax21132:action />
               <ax21132:canBeSold>V</ax21132:canBeSold>
               <ax21132:cashCardAllowed>S</ax21132:cashCardAllowed>
               <ax21132:changeReturn>N</ax21132:changeReturn>
               <ax21132:companyName>NXG</ax21132:companyName>
               <ax21132:destination>1776</ax21132:destination>
               <ax21132:destinationCountryCode>9</ax21132:destinationCountryCode>
               <ax21132:destinationName>DORTMUND</ax21132:destinationName>
               <ax21132:direction>I</ax21132:direction>
               <ax21132:fareIfOutward>27.0</ax21132:fareIfOutward>
               <ax21132:fareIfReturn>54.0</ax21132:fareIfReturn>
               <ax21132:frequencies xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true" />
               <ax21132:isDayReturn>N</ax21132:isDayReturn>
               <ax21132:legs type="com.inforcyl.services.v_1_2.journeysearch.output.journeysearch_output.journey.Leg">
                  <ax21134:arrivalDate>01/01/2014</ax21134:arrivalDate>
                  <ax21134:arrivalTime>09:30</ax21134:arrivalTime>
                  <ax21134:companyName>NXG</ax21134:companyName>
                  <ax21134:companyService>655</ax21134:companyService>
                  <ax21134:containAditionalServices>S</ax21134:containAditionalServices>
                  <ax21134:departureDate>01/01/2014</ax21134:departureDate>
                  <ax21134:departureTime>06:30</ax21134:departureTime>
                  <ax21134:destination>1776</ax21134:destination>
                  <ax21134:destinationName>DORTMUND</ax21134:destinationName>
                  <ax21134:flight>6</ax21134:flight>
                  <ax21134:givePax type="com.inforcyl.services.v_1_2.journeysearch.output.journeysearch_output.journey.leg.GivePax">
                     <ax21137:childAlone>0</ax21137:childAlone>
                     <ax21137:pMR>0</ax21137:pMR>
                  </ax21134:givePax>
                  <ax21134:headerDate>01/01/2014</ax21134:headerDate>
                  <ax21134:headerTime>06:30</ax21134:headerTime>
                  <ax21134:interchangeType />
                  <ax21134:observations />
                  <ax21134:origin>1797</ax21134:origin>
                  <ax21134:originName>FRANKFURT</ax21134:originName>
                  <ax21134:product>1</ax21134:product>
                  <ax21134:seats>99</ax21134:seats>
                  <ax21134:service>9151</ax21134:service>
                  <ax21134:serviceCompanyName>NXG</ax21134:serviceCompanyName>
                  <ax21134:serviceDesc />
                  <ax21134:serviceType>N</ax21134:serviceType>
                  <ax21134:ticketCompany>655</ax21134:ticketCompany>
               </ax21132:legs>
               <ax21132:numberOfLegs>0</ax21132:numberOfLegs>
               <ax21132:operationDatabase>01</ax21132:operationDatabase>
               <ax21132:origin>1797</ax21132:origin>
               <ax21132:originCountryCode>9</ax21132:originCountryCode>
               <ax21132:originName>FRANKFURT</ax21132:originName>
               <ax21132:promotions type="com.inforcyl.services.v_1_2.journeysearch.output.journeysearch_output.journey.Promotion">
                  <ax21134:fareIfOutward>25.0</ax21134:fareIfOutward>
                  <ax21134:fareIfReturn>50.0</ax21134:fareIfReturn>
                  <ax21134:promotion>A</ax21134:promotion>
                  <ax21134:promotionCode>NX1a</ax21134:promotionCode>
                  <ax21134:promotionDescription>special offer</ax21134:promotionDescription>
               </ax21132:promotions>
               <ax21132:returnOpen>N</ax21132:returnOpen>
               <ax21132:salePointArrival>1906</ax21132:salePointArrival>
               <ax21132:salePointDeparture>1906</ax21132:salePointDeparture>
               <ax21132:saleQuery>V</ax21132:saleQuery>
               <ax21132:seasonTicketAllowed>N</ax21132:seasonTicketAllowed>
               <ax21132:takes>300</ax21132:takes>
               <ax21132:trafficType>1</ax21132:trafficType>
               <ax21132:type />
               <ax21132:warning />
            </ax21130:journeys>
            <ax21130:journeys type="com.inforcyl.services.v_1_2.journeysearch.output.journeysearch_output.Journey">
               <ax21132:action />
               <ax21132:canBeSold>V</ax21132:canBeSold>
               <ax21132:cashCardAllowed>S</ax21132:cashCardAllowed>
               <ax21132:changeReturn>N</ax21132:changeReturn>
               <ax21132:companyName>NXG</ax21132:companyName>
               <ax21132:destination>1776</ax21132:destination>
               <ax21132:destinationCountryCode>9</ax21132:destinationCountryCode>
               <ax21132:destinationName>DORTMUND</ax21132:destinationName>
               <ax21132:direction>I</ax21132:direction>
               <ax21132:fareIfOutward>27.0</ax21132:fareIfOutward>
               <ax21132:fareIfReturn>54.0</ax21132:fareIfReturn>
               <ax21132:frequencies xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:nil="true" />
               <ax21132:isDayReturn>N</ax21132:isDayReturn>
               <ax21132:legs type="com.inforcyl.services.v_1_2.journeysearch.output.journeysearch_output.journey.Leg">
                  <ax21134:arrivalDate>01/01/2014</ax21134:arrivalDate>
                  <ax21134:arrivalTime>15:30</ax21134:arrivalTime>
                  <ax21134:companyName>NXG</ax21134:companyName>
                  <ax21134:companyService>655</ax21134:companyService>
                  <ax21134:containAditionalServices>S</ax21134:containAditionalServices>
                  <ax21134:departureDate>01/01/2014</ax21134:departureDate>
                  <ax21134:departureTime>12:30</ax21134:departureTime>
                  <ax21134:destination>1776</ax21134:destination>
                  <ax21134:destinationName>DORTMUND</ax21134:destinationName>
                  <ax21134:flight>7</ax21134:flight>
                  <ax21134:givePax type="com.inforcyl.services.v_1_2.journeysearch.output.journeysearch_output.journey.leg.GivePax">
                     <ax21137:childAlone>0</ax21137:childAlone>
                     <ax21137:pMR>0</ax21137:pMR>
                  </ax21134:givePax>
                  <ax21134:headerDate>01/01/2014</ax21134:headerDate>
                  <ax21134:headerTime>12:30</ax21134:headerTime>
                  <ax21134:interchangeType />
                  <ax21134:observations />
                  <ax21134:origin>1797</ax21134:origin>
                  <ax21134:originName>FRANKFURT</ax21134:originName>
                  <ax21134:product>1</ax21134:product>
                  <ax21134:seats>99</ax21134:seats>
                  <ax21134:service>9151</ax21134:service>
                  <ax21134:serviceCompanyName>NXG</ax21134:serviceCompanyName>
                  <ax21134:serviceDesc />
                  <ax21134:serviceType>N</ax21134:serviceType>
                  <ax21134:ticketCompany>655</ax21134:ticketCompany>
               </ax21132:legs>
               <ax21132:numberOfLegs>0</ax21132:numberOfLegs>
               <ax21132:operationDatabase>01</ax21132:operationDatabase>
               <ax21132:origin>1797</ax21132:origin>
               <ax21132:originCountryCode>9</ax21132:originCountryCode>
               <ax21132:originName>FRANKFURT</ax21132:originName>
               <ax21132:promotions type="com.inforcyl.services.v_1_2.journeysearch.output.journeysearch_output.journey.Promotion">
                  <ax21134:fareIfOutward>25.0</ax21134:fareIfOutward>
                  <ax21134:fareIfReturn>50.0</ax21134:fareIfReturn>
                  <ax21134:promotion>A</ax21134:promotion>
                  <ax21134:promotionCode>NX1a</ax21134:promotionCode>
                  <ax21134:promotionDescription>special offer</ax21134:promotionDescription>
               </ax21132:promotions>
               <ax21132:returnOpen>N</ax21132:returnOpen>
               <ax21132:salePointArrival>1906</ax21132:salePointArrival>
               <ax21132:salePointDeparture>1906</ax21132:salePointDeparture>
               <ax21132:saleQuery>V</ax21132:saleQuery>
               <ax21132:seasonTicketAllowed>N</ax21132:seasonTicketAllowed>
               <ax21132:takes>300</ax21132:takes>
               <ax21132:trafficType>1</ax21132:trafficType>
               <ax21132:type />
               <ax21132:warning />
            </ax21130:journeys>
            <ax21130:operationDatabase>01</ax21130:operationDatabase>
         </ns:return>
      </ns:executeResponse>
   </soapenv:Body>
</soapenv:Envelope>

我试图通过谷歌使用我发现的几种方法从PHP获取旅程数据,但我无法让它工作,很可能是由于命名空间。我必须承认我不是XML专家。在搜索这个网站时,我发现了一个与this HOWTO链接的类似帖子。

我打算为我的XML创建等效代码,但我没有获得旅程信息:

// $result_xml is the XML posted above
$xml_obj = simplexml_load_string($result_xml, NULL, NULL, "http://schemas.xmlsoap.org/soap/envelope/");

  if ($xml_obj === false) {
    show_error("Failed loading XML");
    foreach(libxml_get_errors() as $error) {
        echo "ERROR: \t", $error->message;
    }
  }
  else {
    show_info("XML loaded. Children: ");
    foreach($xml_obj->children() as $child) {
      print("Child: " . var_dump($child) . "");
    }
    show_info("Attributes: ");
    foreach($xml_obj->attributes() as $attribute) {
      print("Attributes: " . var_dump($attribute) . "");
    }
    show_info("Journeys: ");
    foreach($xml_obj->journeys as $journey) {
      $ns_dc = $journey->children('http://output.journeysearch.v_1_2.services.inforcyl.com/xsd');
      print("Journey cashCardAllowed: " . $ns_dc->cashCardAllowed  . "");
    }

    //echo "As XML: " . $xml_obj->asXML();  // This returns the XML just fine
  }

这给了我以下输出:

XML loaded. Children:
Attributes:
Journeys:

有人可以帮我使用XML命名空间来获取旅程数据吗?我是否还需要为每个子元素指定命名空间?

我需要:

<ax21132:fareIfOutward>27.0</ax21132:fareIfOutward>
<ax21132:fareIfReturn>54.0</ax21132:fareIfReturn>
<ax21134:departureDate>01/01/2014</ax21134:departureDate>
<ax21134:departureTime>12:30</ax21134:departureTime>

0 个答案:

没有答案