读取SOAP XML文件


Read SOAP XML file

我有SOAP XML文件(data.XML),其中包含以下数据,实际上它是一些API的响应:

<?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) 如何获取"拍卖"标签信息并将其保存到数组中?

用糟糕的风格编写,但解决了问题。只删除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);

您需要使用simplexml_load_file

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

最终,我得到了解决方案。我刚刚删除了'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";
       }                
    }

现在,我可以遍历每个属性了。感谢