473,396 Members | 1,799 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.

Parsing XML Data to variable

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" encoding="UTF-8" ?>
<shareprice>
<exchange>LSE</exchange>
<symbol>BWD</symbol>
<price>440.00</price>
<date>19 Aug</date>
<time>11:20am</time>
<change>+1.00</change>
<percent>+0.23</percent>
<volume>1250</volume>
</shareprice>

All I need is the data to be extracted into the following variables

$exchange
$symbol
$price
$date
$time
$change
$percent
$volume

Does anyone have any help on this please?
Jul 16 '05 #1
4 5282
Seriously Bruno!! I posted on the 14th to alt.comp.lang.php and two people
suggested solutions which I have been struggling with since then. I'm not at
all familiar with PHP so that could be the reason I am struggling!

I'll try your advice and let you know how I get on. Thank you
"Bruno Desthuilliers" <bd***********@removeme.free.fr> wrote in message
news:3f***********************@news.free.fr...
Andrew Banks wrote:
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" encoding="UTF-8" ?>
<shareprice>
<exchange>LSE</exchange>
<symbol>BWD</symbol>
<price>440.00</price>
<date>19 Aug</date>
<time>11:20am</time>
<change>+1.00</change>
<percent>+0.23</percent>
<volume>1250</volume>
</shareprice>

All I need is the data to be extracted into the following variables

$exchange
$symbol
$price
$date
$time
$change
$percent
$volume

Does anyone have any help on this please?


Q&D style, so you get the idea :

$current_tag = null;
$datas = Array();

function onStartElement($parser, $tagname, $att)
{
global $current_tag;
$current_tag = $tagname;
}

function onCData($parser, $cdata)
{
global $current_tag;
global $datas;

if ($current_tag) {
$data[$current_tag] = $cdata;
}
}

function onEndElement($parser, $tagname)
{
global $current_tag;
$current_tag = null;
}

$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "onStartElement", "onEndElement");
xml_set_character_data_handler($xml_parser, "onCData");

$file = "path/to/xmlfile.xml";

if (!($fp = fopen($file, "r"))) {
die("could not open XML input");
}

while ($data = fread($fp, 4096)) {
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);

Now your datas are in $datas :
$datas['volume'], $datas['symbol'], etc...

(not tested, but except for possible typos, it should work).

Just one thing : I can't believe you've really "been struggling with
this for the past 2 days and have tried various solutions". It took me
3'30'' to write the above code.

HTH
Bruno

Jul 16 '05 #2
Now it's tested and it works. But it's still Q&D.
What's meant by Q&D?

BTW, and a bit OT : could you please answer below the post, not above,
and keep only releving fragments ? TIA.


Thanks Bruno - different NGs have different rules. Not really a regular on
this NG but will try to post correct from here on!


Jul 16 '05 #3
Andy Hassall wrote:
On Tue, 19 Aug 2003 20:40:56 GMT, "Andrew Banks"
<ba****@blablablueyonder.co.uk> wrote:

Now it's tested and it works. But it's still Q&D.


What's meant by Q&D?

Quick & Dirty ?

And the winner iiiiiis.... !-)

Jul 16 '05 #4
Andrew Banks wrote:
Now it's tested and it works. But it's still Q&D.

What's meant by Q&D?


Quick and Dirty.

As you can see, this code uses globals, does not handle errors, or in a
very primitive way, does not check the validity of the xml format, etc.
I would definitively not put such a code on production 'as is'.

BTW, and a bit OT : could you please answer below the post, not above,
and keep only releving fragments ? TIA.

Thanks Bruno - different NGs have different rules.


These two are part of the 'standard usenet rules set'.
Not really a regular on
this NG
Which one ? alt.c.l.php or c.l.php ?-)
but will try to post correct from here on!

Thanks.

Bruno

Jul 16 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Rob Archibald | last post by:
I have a back end system that passed data to me in tabular XML format so that it can be easily loaded into a Data Dynamics SharpGrid. The native format for data in SharpGrid is like the following:...
7
by: Michael R. Copeland | last post by:
I'm processing a control file comprised of many types of lines, with some containing variable data. I have a problem parsing the following data: 18 12.2 7.145 6.214 Phase distances First,...
8
by: Eric Anderson | last post by:
I have some files that sit on a FTP server. These files contain data stored in a tab-separated format. I need to download these files and insert/update them in a MySQL database. My current basic...
12
by: Klaus Alexander Seistrup | last post by:
Hi group, I am new to xgawk (and seemingly to xml also), and I've been struggling all afternoon to have xgawką parsing an XHTML file containing a hCard˛, without luck. I wonder if you guys...
15
by: Lone Wolf | last post by:
I'm trying to get data through my serial port from a CMUcam. This gizmo tracks a color and returns a packet of data. The packet has nine data points (well, really eight since the first point is...
3
by: mtuller | last post by:
Alright. I have tried everything I can find, but am not getting anywhere. I have a web page that has data like this: <tr > <td headers="col1_1" style="width:21%" > <span class="hpPageText"...
2
by: Gary42103 | last post by:
Hi I need Perl Script to do Data Parsing using existing data files. I have my existing data files in the following directory: Directory Name: workfs/ams Data File Names: 20070504.dat,...
13
by: Chris Carlen | last post by:
Hi: Having completed enough serial driver code for a TMS320F2812 microcontroller to talk to a terminal, I am now trying different approaches to command interpretation. I have a very simple...
9
by: Jasper | last post by:
Hi, I have multiple data files which need parsing in realtime so high performance is *crucial*. I dont have a format definition, but from what I can see there is a hierarchy of data. Each...
3
by: GazK | last post by:
I have been using an xml parsing script to parse a number of rss feeds and return relevant results to a database. The script has worked well for a couple of years, despite having very crude...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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.