2013-08-16 79 views
0

我已經包含下面的數據SOAP XML文件(data.xml中),其實,這是一些API的響應:讀取SOAP XML文件

<?xml version="1.0" encoding="utf-8"?> 
    <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> 
      <GetAuctionList2Response xmlns="GdAuctionsBiddingWSAPI"> 
       <GetAuctionList2Result> 
        <AuctionList IsValid="True" TotalRecords="98"> 
         <Auction ID="112910726" Name="SOFTWARE-SERVER.ORG" Traffic="2" BidCount="0" Price="$9 USD" ValuationPrice="-" TimeLeft="9H 36M " RowID="1"/> 
         <Auction ID="112926015" Name="SOFTWARELAWSUITS.COM" Traffic="0" BidCount="0" Price="$8 USD" ValuationPrice="-" TimeLeft="9H 39M " RowID="2"/> 
         <Auction ID="113234131" Name="SOFTWARECRAFTORY.COM" Traffic="4" BidCount="0" Price="$11 USD" ValuationPrice="-" TimeLeft="9H 53M " RowID="3"/> 
         <Auction ID="112906125" Name="SOFTWARESYSTEMS.CO" Traffic="0" BidCount="0" Price="$8 USD" ValuationPrice="-" TimeLeft="10H 15M " RowID="4"/> 
         <Auction ID="112692380" Name="SOFTWAREREPAIR.ORG" Traffic="0" BidCount="0" Price="$5 USD" ValuationPrice="-" TimeLeft="10H 46M " RowID="5"/> 
        </AuctionList> 
       </GetAuctionList2Result> 
      </GetAuctionList2Response> 
     </soap:Body> 
    </soap:Envelope> 

當我嘗試讀它,它不工作並拋出錯誤。 的第一次嘗試:

$doc = new DOMDocument(); 
$doc->load('data.xml'); 

$auctionList = $doc->getElementsByTagName("AuctionList"); 

foreach($auctionList as $list) { 
    $names = $list->getElementsByTagName("Auction"); 
    echo "<b>$name\n</b><br>"; 
} 

錯誤:

Notice: DOMDocument::load(): xmlns: URI GdAuctionsBiddingWSAPI is not absolute in file:///C:/xampp/htdocs/adam_auction/data.xml, line: 4 in C:\xampp\htdocs\adam_auction\readfile.php on line 5 

Notice: Undefined variable: name in C:\xampp\htdocs\adam_auction\readfile.php on line 13 

第二次嘗試:

$s = simplexml_load_string('http://localhost/adam_auction/data.xml'); 
print_r($s); 

錯誤:

Warning: simplexml_load_string(): Entity: line 1: parser error : Start tag expected, '<' not found in C:\xampp\htdocs\adam_auction\readfile.php on line 4 

Warning: simplexml_load_string(): http://localhost/adam_auction/data.xml in C:\xampp\htdocs\adam_auction\readfile.php on line 4 

Warning: simplexml_load_string():^in C:\xampp\htdocs\adam_auction\readfile.php on line 4 

誰能幫我什麼是錯的SOAP XML文件?

1)XML文件是否無效? 2)如何閱讀並保存到一些變量? 3)如何獲得'Auction'標籤信息並將其保存到數組中?

+0

你爲什麼不使用'SoapClient' – DevZer0

+0

@ DevZer0,我沒問SoapClient的http://stackoverflow.com/questions/18229478/soap-request-using-soapclient,但無法通過SoapClient。你能幫助如何使SoapClient請求?你可以從上面的鏈接找到我的代碼。 –

回答

0

最終,我得到了解決方案。我剛剛刪除了'xmlns =「GdAuctionsBiddingWSAPI」'命名空間,它的工作!現在

// getting response from API despite saving it to file 
    $response= str_replace('GdAuctionsBiddingWSAPI', '', $response); 

    $xml = simplexml_load_string($response); 

    $auction_list = $xml->xpath('//Auction'); 

    foreach ($auction_list as $item) { 

     foreach($item->attributes() as $a => $b) { 
     echo $a,'="',$b,"\"\n"; 
     }     
    } 

,我」能夠通過每個attribute.Thanks迭代

-1

您需要使用simplexml_load_file

$s = simplexml_load_file('http://localhost/adam_auction/data.xml'); 
print_r($s); 
+0

simplexml_load_file和simplexml_load_string都會產生相同的錯誤。 –

+0

任何幫助如何使它通過SoapClient,這裏是代碼http://stackoverflow.com/questions/18229478/soap-request-using-soapclient –

0

寫在惡劣的風格,但解決問題。只刪除Soap Envelope的基本標籤,加載XML並用'@'打印。

$response = file_get_contents($file_path); 
$response = explode(PHP_EOL, $response); 

$skip = array(0, 1, 2, count($response)-1, count($response)-2); 
$xml = array(); 

for($i=0;$i<count($response);$i++){ 
    if(!in_array($i, $skip)){ 
     $xml[] = $response[$i]; 
    } 
} 

$xml = implode('', $xml); 

@print_r(simplexml_load_string($xml)); 
// or 
@$obj = simplexml_load_string($xml);