By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
438,664 Members | 1,656 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 438,664 IT Pros & Developers. It's quick & easy.

Get XML data and convert to PHP variable

P: 16
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?

Aug 28 '08 #1
Share this Question
Share on Google+
2 Replies

code green
Expert 100+
P: 1,726
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
Aug 28 '08 #2

Expert 100+
P: 1,584
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.

if (!$xmlObj=simplexml_load_file('products.xml')){
echo 'Error reading XML file';
foreach($xmlObj as $product){

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


please read the documentation for simplexml_load_file
Aug 29 '08 #3

Post your reply

Sign in to post your reply or Sign up for a free account.