1

我有包含以下数据的 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) 如何获取“拍卖”标签信息并将其保存到数组中?

4

3 回答 3

1

写得不好,但解决了问题。仅删除 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);
于 2013-08-16T12:25:23.637 回答
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";
       }                
    }

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

于 2013-08-16T13:15:06.423 回答
0

你需要使用simplexml_load_file

$s = simplexml_load_file('http://localhost/adam_auction/data.xml');
print_r($s);
于 2013-08-16T07:39:13.443 回答