读取SOAP XML文件

时间:2013-08-16 07:26:33

标签: php xml arrays soap

我的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)如何获取“拍卖”标签信息并将其保存到阵列中?

3 个答案:

答案 0 :(得分: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);

答案 1 :(得分:0)

您需要使用simplexml_load_file

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

答案 2 :(得分: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";
       }                
    }

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