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

Parsing XML Data to variable

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.