Fetching data form xml

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Fetching data form xml

Post by itsmani1 »

Hi all

I am using "EventInventory Web Services" here is php code for making the call:

Code: Select all

<?

// include the SOAP classes
require_once('nusoap.php');

// define parameter array
$param = array( 'APPCLIENT_ID' => '*CLIENTID*','EVENT_ID' => '','STARTDATE' => '','INCDAYS' => '');

// define path to server application
// Do not hard code the URL here. Get it from a central place like 
// configuration file. The below URL is hard coded just for example
// purpose.
$serverpath ='http://services.Preview.EventInventory.com/webservices/TicketSearch.asmx';

//define method namespace
$namespace="http://www.eventinventory.com/webservices/";

// create client object
$soapclient = new soapclient($serverpath);

//set soap Action
$soapAction='http://www.eventinventory.com/webservices/GetVenueList';

//to see debug messages
//$soapclient -> debug_flag = 1;

 // make the call
$result = $soapclient->call('GetVenueList',$param,$namespace,$soapAction);

// if a fault occurred, output error info
if (isset($fault)) {
        print "Error: ". $fault;
        }
else if ($result) {

        if ($result[faultstring])
        {
         print"<h2>Error:</h2>
         $result[faultstring]";
        }
        else   //show results
        {
  $root=$result[ROOT];
              $data = $root[DATA];
  $row = $data[row];             

for($i=0;$i<count($row);$i++)
             {
                 $venueId = $row[$i]['!VID'];
                 $venueName = $row[$i]['!Name'];
                 $city = $row[$i]['!City'];
                 $state = $row[$i]['!State'];
                 $zip = $row[$i]['!ZipCode'];
                 print"$venueId : $venueName : $city : state :$zip<br>";
             }
        }
}
else {
        print "No result";
        }
// kill object
unset($soapclient);

?>
All data will be returned in “row” tags with attributes containing the data for each field. Example:

<DATA xmlns:sql="urn:schemas-microsoft-com:xml-sql">
<row THISID="2" THISNAME="Alabama" />
<row THISID="55" THISNAME="Alberta" />
</DATA>


As it shows it will return data in the form on an XML file, i don't know to fetch data and dispay it on my page.
Please help me.

Thanks
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

Here is my XML file example.xml

Code: Select all

<?xml version="1.0"?>
<room>
   <dining-room>
      <manufacturer>The Wood Shop</manufacturer>
      <table type="round" wood="maple">
         <price>$199.99</price>
      </table>
      <chair wood="maple">
         <quantity>6</quantity>
         <price>$39.99</price>
      </chair>
   </dining-room>
   <dining-room1>
      <manufacturer>Shop Makers</manufacturer>
      <table type="sq" wood="maple">
         <price>$222.99</price>
      </table>
      <chair wood="maple">
         <quantity>6</quantity>
         <price>$139.99</price>
      </chair>
   </dining-room1>
</room>
here is my php code:

Code: Select all

<?php
if(file_exists('example.xml'))
{
	$xml = simplexml_load_file('example.xml');
	var_dump($xml);
}
else
{
	exit('Failed to open example.xml.');
}
?>
as a result of this code i am been able to get following result:
object(SimpleXMLElement)#1 (2) { ["dining-room"]=> object(SimpleXMLElement)#2 (3) { ["manufacturer"]=> string(13) "The Wood Shop" ["table"]=> object(SimpleXMLElement)#4 (1) { ["price"]=> string(7) "$199.99" } ["chair"]=> object(SimpleXMLElement)#5 (2) { ["quantity"]=> string(1) "6" ["price"]=> string(6) "$39.99" } } ["dining-room1"]=> object(SimpleXMLElement)#3 (3) { ["manufacturer"]=> string(11) "Shop Makers" ["table"]=> object(SimpleXMLElement)#6 (1) { ["price"]=> string(7) "$222.99" } ["chair"]=> object(SimpleXMLElement)#7 (2) { ["quantity"]=> string(1) "6" ["price"]=> string(7) "$139.99" } } }

now i don't know how to fetch values?
any idea like how to get price? etc.

thanks
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

let me see it.

thanks for the reply.
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

is there anyway so that we can traverse the whole tree in an xml file.


or we can fetch all values of a single property like price etc.


thanks
User avatar
Burrito
Spockulator
Posts: 4715
Joined: Wed Feb 04, 2004 8:15 pm
Location: Eden, Utah

Post by Burrito »

you'll have to loop over all of the elements (as objects) to gather the data from them.
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

You can select elements/attributes/nodes via xpath

Code: Select all

$doc = simplexml_load_file('example.xml');

$nodes = $doc->xpath('//price');

echo count($nodes), " price elements found<br />\n";
foreach($nodes as $n) {
	echo $n, "<br />\n";
}
It's a more complex subject, try http://www.w3.org/ and google
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

volka wrote:You can select elements/attributes/nodes via xpath

Code: Select all

$doc = simplexml_load_file('tst.xml');

$nodes = $doc->xpath('//price');

echo count($nodes), " price elements found<br />\n";
foreach($nodes as $n) {
	echo $n, "<br />\n";
}
It's a more complex subject, try http://www.w3.org/ and google
Reasult after execution:

Warning: simplexml_load_file() [function.simplexml-load-file]: I/O warning : failed to load external entity "tst.xml" in /opt/lampp/htdocs/projects/Mannan/xml.php on line 3

Fatal error: Call to a member function xpath() on a non-object in /opt/lampp/htdocs/projects/Mannan/xml.php on line 5
User avatar
volka
DevNet Evangelist
Posts: 8391
Joined: Tue May 07, 2002 9:48 am
Location: Berlin, ger

Post by volka »

if there is no file tst.xml then simplexml_load_file cannot load the xml file. Do you have a file tst.xml?

btw, I wrote example.xml not tst.xml. Please don't quote me wrong.
User avatar
itsmani1
Forum Regular
Posts: 791
Joined: Mon Sep 29, 2003 2:26 am
Location: Islamabad Pakistan
Contact:

Post by itsmani1 »

oooooh sorry man i did a silly mistake.\




its working now.

thanks
Post Reply