473,396 Members | 1,703 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,396 software developers and data experts.

Get XML data and convert to PHP variable

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?

Thanks
Aug 28 '08 #1
2 9810
code green
1,726 Expert 1GB
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
ak1dnar
1,584 Expert 1GB
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
Aug 29 '08 #3

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

Similar topics

4
by: Andrew Banks | last post by:
I've been struggling with this for the past 2 days and have tried various solutions. I need to parse the following XML file and extract the data into variables <?xml version="1.0"...
0
by: AJ Shankar | last post by:
Hi, In the C API, is there any way to execute a query, store all the results, and somehow find the maximum realized width for each of the fields before fetching each row? Otherwise there is no...
2
by: Ron Sorrell | last post by:
I have a requirment to take data from a large set of tables where the total number of these tables may change on a regular baisis and output into another table. All the tables willl have the...
9
by: Rodusa | last post by:
I am trying to assign @sql variable to @total, where @sql is a sql statement stored on the database, however what I am getting is its string value and not its calcuation. Could anybody help? ...
3
by: Cogswell | last post by:
i have a quick ? i have a variable that is named dynamically by combining values into a string, how would i then get the value of the string formname = "document.form" + count +...
2
by: staeri | last post by:
I need to input a stored procedure parameter as an integer but the conversion doesn't work. I get the error message "Input string was not in a correct format." I'm using the following code: Dim...
3
by: toodi4 | last post by:
I'm using a javascript that hides and unhides text based on a button click. It works great across static fields on a form. The problem I have is that I'm trying to hide and unhide various fields...
5
by: keeps21 | last post by:
Ok, so here goes. I need to retrieve data, (ie. id, title, introtext.) from my database and store each in a variable. I then need to repeat this for the next 4 records. So I would have 5...
6
by: tgmcnaughton | last post by:
I have a javascript running on a page hosted by googlepages at: http://tgmcnaughton.googlepages.com/map2.htm It uses the google map api to draw a map and plot a marker at a particular latitude and...
5
by: Gernot Frisch | last post by:
Hi, I've got to write a BASIC to C++ converter and came across the problem of the DATA statement. I must provide a way of storing: DATA 5.123, "abc", ... in C++. my first guess was this:
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.