Irfan
Irfan

Reputation: 5062

Read SOAP XML file

I've SOAP XML file (data.xml) which contains below data, In fact it is response of some 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>

When I try to read it, It does not work and throws error. First Try:

$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>";
}

Error:

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

Second Try:

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

Error:

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

Can anyone help me what is wrong with SOAP XML file?

1) Is the XML file invalid? 2) How to read it and save it to some variable? 3) How to get 'Auction' tag information and save it into array?

Upvotes: 1

Views: 4167

Answers (3)

Irfan
Irfan

Reputation: 5062

Eventually, I got the solution. I just removed the 'xmlns="GdAuctionsBiddingWSAPI"' namespace and it worked!

    // 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";
       }                
    }

Now, I'm' able to iterate through each attribute.Thanks

Upvotes: 0

Maytyn
Maytyn

Reputation: 122

Written in bad style but solves problem. Remove just Soap Envelope's basic tags, load XML and print with '@'.

$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);

Upvotes: 1

DevZer0
DevZer0

Reputation: 13535

You need to use simplexml_load_file

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

Upvotes: 0

Related Questions