Help | Site Map
Connecting Tech Pros Worldwide
Reply
 
LinkBack Thread Tools
  #1  
Old August 28th, 2008, 05:25 PM
Newbie
 
Join Date: Sep 2007
Location: USA
Posts: 16
Default Get XML data and convert to PHP variable

I have a XML file that looks like this:
Expand|Select|Wrap|Line Numbers
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <products>
  3.   <product>
  4.     <sku>10001</sku>
  5.     <qty>3</qty>
  6.     <price>822.51</price>
  7.   </product>
  8.   <product>
  9.     <sku>10002</sku>
  10.     <qty>1</qty>
  11.     <price>320.86</price>
  12.   </product>
  13.   <product>
  14.     <sku>10003</sku>
  15.     <qty>0</qty>
  16.     <price>498.91</price>
  17.   </product>
  18. </products>
I want to get the <price>....</price> convert it to a php variable calculate a new nymber based on that price then add a new element <price2>...</price2> to each product. I know how to calculate and add new element. The only part that I don't know is how to get the price for each product and convert it to a variable.
Is there a simple php code that could do this?

Thanks
Reply
  #2  
Old August 28th, 2008, 05:55 PM
code green's Avatar
Expert
 
Join Date: Mar 2007
Posts: 839
Default

Not really,
There are a couple of XML components built into php.
I recommend studying a tutorial about DOM for XML using php.
Sorry, don't have a link but just Google
Expand|Select|Wrap|Line Numbers
  1. php DOM xml tutorial
Reply
  #3  
Old August 29th, 2008, 06:32 AM
ak1dnar's Avatar
Moderator
 
Join Date: Jan 2007
Posts: 1,400
Default

You can read your XML file data as described in below example, lets say your xml is products.xml. just add these lines to a php scripts which is placed in the same directory as your xml file.

[PHP]<?php
if (!$xmlObj=simplexml_load_file('products.xml')){
echo 'Error reading XML file';
}
foreach($xmlObj as $product){

echo 'Price : ' . $product->price . '<br />';

}
?>[/PHP]

please read the documentation for simplexml_load_file
Reply
Reply

Bookmarks

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are Off
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On

What is Bytes?

We are a network of experts and professionals in IT and software development that help one another with answers to tough questions and share insights. Get the best answers to your questions from over network members.
Post your question now . . .
It's fast and it's free

Popular Articles