Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations derfloh on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Parse XML files

Status
Not open for further replies.

tewari68

Programmer
Jan 25, 2005
87
US
Hi,
Appreciate if anybody can let me know how to parse the following xml file in php
Code:
<?xml version="1.0" encoding="utf-8" ?>
<OrderList>
<Order ID="438" Wholesale_Fulfillment_ID=""></Order>
<Order ID="464" Wholesale_Fulfillment_ID=""></Order>
<Order ID="506" Wholesale_Fulfillment_ID=""></Order>
<Order ID="515" Wholesale_Fulfillment_ID=""></Order>
<Order ID="587" Wholesale_Fulfillment_ID=""></Order>
<Order ID="619" Wholesale_Fulfillment_ID=""></Order>
<Order ID="627" Wholesale_Fulfillment_ID=""></Order>
<Order ID="628" Wholesale_Fulfillment_ID=""></Order>
<Order ID="629" Wholesale_Fulfillment_ID=""></Order>
<Order ID="637" Wholesale_Fulfillment_ID=""></Order>
<Order ID="687" Wholesale_Fulfillment_ID=""></Order>
<Order ID="689" Wholesale_Fulfillment_ID=""></Order>
<Order ID="695" Wholesale_Fulfillment_ID=""></Order>
<Order ID="696" Wholesale_Fulfillment_ID=""></Order>
<Order ID="699" Wholesale_Fulfillment_ID=""></Order>
<Order ID="773" Wholesale_Fulfillment_ID=""></Order>
<Order ID="801" Wholesale_Fulfillment_ID=""></Order>
</OrderList>
I need to get the order id numbers in an array.
Appreciate any help.
Thanks,
tewari
 
Hi Miros,
Here the problem is that there is no data between the start and end tags but it is included within the start tag as
Code:
<?xml version="1.0" encoding="utf-8" ?>
<OrderList>
<Order ID="438" Wholesale_Fulfillment_ID=""></Order>
<Order ID="464" Wholesale_Fulfillment_ID=""></Order>
<Order ID="506" Wholesale_Fulfillment_ID=""></Order>
<Order ID="515" Wholesale_Fulfillment_ID=""></Order>
<Order ID="587" Wholesale_Fulfillment_ID=""></Order>
<Order ID="619" Wholesale_Fulfillment_ID=""></Order>
<Order ID="627" Wholesale_Fulfillment_ID=""></Order>
<Order ID="628" Wholesale_Fulfillment_ID=""></Order>
<Order ID="629" Wholesale_Fulfillment_ID=""></Order>
<Order ID="637" Wholesale_Fulfillment_ID=""></Order>
<Order ID="687" Wholesale_Fulfillment_ID=""></Order>
<Order ID="689" Wholesale_Fulfillment_ID=""></Order>
<Order ID="695" Wholesale_Fulfillment_ID=""></Order>
<Order ID="696" Wholesale_Fulfillment_ID=""></Order>
<Order ID="699" Wholesale_Fulfillment_ID=""></Order>
<Order ID="773" Wholesale_Fulfillment_ID=""></Order>
<Order ID="801" Wholesale_Fulfillment_ID=""></Order>
</OrderList>
I am new to xml parsing in php can you help me a little bit more on how to get the order ids.

Thanks,
tewari
 
You simply access the attributes instead of the values.
 
Basically in this routine:
Code:
function startHandler($parser, $name, $attributes)
   {
       $data['name'] = $name;
       if ($attributes) { $data['attributes'] = $attributes; }
       $this->data[] = $data;
   }

you'd do:
Code:
if ($attributes) $this->orders[] = GetID($attributes);

GetID would parse the $attributes string and return the ID.

 
Hi Miros,
I replaced the code with what you have suggested, however I keep getting call to undefined function getid() error.
Do I need to further define the GetID function?

Appreciate your help.

Thanks,
tewari
 
Yes, you write GetID. It would use indexOf to find ID=" and the following ", then do a substring to get the actual ID.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top