2014-11-04 102 views
1

我有一個xml。如附件所示.. 如何通過PHP讀取其中的內容用php中的屬性讀取xml

http://agency.lastminute-hr.com/slike/xml_read1.jpg 「XML」

現在我看到這個XML:

<? 

    $request   = new SimpleXMLElement('<HotelInfoRequest/>'); 
    $request->Username = 'SERUNCD'; 
    $request->Password = 'TA78UNC'; 
    $request->HotelID = '123'; 



    $url = "http://wl.filos.com.gr/services/WebService.asmx/HotelInfo"; 
    $result = simplexml_load_file($url . "?xml=" . urlencode($request->asXML())); 



    function RecurseXML($xml,$parent="") 
    { 
     $child_count = 0; 
     foreach($xml as $key=>$value) 
     { 
      $child_count++; 

      if(RecurseXML($value,$parent.".".$key) == 0) // no childern, aka "leaf node" 
      { 

      print($parent . "." . (string)$key . " = " . (string)$value . "<BR>\n");  
      }  
     } 
     return $child_count; 
    } 

    RecurseXML($result); 
?> 

這個結果看:

.. 
.. 
.Response.Hotels.Hotel.Rooms.Room.Quantity = 0 
.Response.Hotels.Hotel.Rooms.Room.MaxPax = 3 
.Response.Hotels.Hotel.Rooms.Room.MinPax = 2 
.Response.Hotels.Hotel.Rooms.Room.RoomType.languages.lang = Room Double Standard 
.Response.Hotels.Hotel.Rooms.Room.RoomType.languages.lang = Camera single standard 
... 
... 

我需要你顯示數值並加以說明

<Rooms> 
    <Room ID = "5556"> 
     <Quantity> 0 </ Quantity> 
     <MaxPax> 3 </ MaxPax> 
     <MinPax> 2 </ MinPax> 
     <Roomtype> </ roomtype> 
</ Room> 
.... 
.. 
+0

的可能重複(http://stackoverflow.com/questions/3577641 ($ xml is leaf){ – Ghost 2014-11-04 07:39:05

回答

1

嘗試(第一部分是僅用於測試):?你如何解析和PHP程序HTML/XML]

$string = <<<XML 
<?xml version='1.0'?> 
<Rooms> 
    <Room ID = "5556"> 
     <Quantity> 0 </Quantity> 
     <MaxPax> 3 </MaxPax> 
     <MinPax> 2 </MinPax> 
     <Roomtype> </Roomtype> 
    </Room> 
</Rooms> 
XML; 

$result = simplexml_load_string($string); 

function RecurseXML($xml,$parent=".Response.Hotels.Hotel.Rooms"){ 
    $children = $xml->children(); 
    if (empty($children)) { 
     print($parent . "." . $xml->nodeName . " = " . (string)$xml . "<BR>\n"); 
    } 
    else { 
     foreach($children as $key=>$value) { 
      RecurseXML($value, $parent . "." . $key); 
     } 
    } 
} 

RecurseXML($result); 
+0

if 「 – sale2211 2014-11-04 08:11:39

+0

M.Page:我得到一個錯誤「if($ xml is leaf){/ how-do-you-parse-and-process-html-xml-in-php) – sale2211 2014-11-04 08:57:27

+0

是的,這就是爲什麼我寫道:這是」僞代碼「。這意味着您必須通過實際測試來指示當前節點是否爲葉節點來替換'is leaf'。我不知道你用什麼來分析你的XML(SimpleXML,DOM XML,...),這就是爲什麼我這樣寫的。 – 2014-11-04 10:29:39