Hi,
I've been reading an article on PHP and XML integration on PHPbuilder and have tried to adapt it to work with some exchange rates from XE.com
The original tutorial's at http://www.phpbuilder.com/annotate/message.php3?id=1024438
An example of the XML is at: -
http://www.xe.com/dfs/sample-gbp.xml
And the code i've bodged together is listed at the end of the post.
I'm toally new to XML and haven't ever got this stuck with PHP before, i'm literally pulling my hair out and not getting any further with this problem.
Could someone please point me in the right direction and lend a hand? any help would be very much appreciated.
P.S... it's the currency information i need not the headers, but i thought i'd start wwith the headers incase it's throwing up an error.
Code below: -
<?php
$open_tags = array(
'STORY' => '<xe-datafeed>',
'TITLE' => '<header>',
'URL' => '<hname>');
$close_tags = array(
'STORY' => '</xe-datafeed>',
'TITLE' => '</header>',
'URL' => '</hname>');
?>
<?php
// handles the attributes for opening tags
// $attrs is a multidimensional array keyed by attribute
// name and having the value of that attribute
function startElement($parser, $name, $attrs=''){
global $open_tags, $temp, $current_tag;
$current_tag = $name;
if ($format = $open_tags[$name]){
switch($name){
case 'header':
echo 'New Story: ';
break;
default:
break;
}
}
}
// $current_tag lets us know what tag we are currently
// dealing with - we use that later in the characterData
// function.
//
// when we see a </STORY> we know that it is time to
// flush our temp variables and prepare to move onto
// the next one
function endElement($parser, $name, $attrs=''){
global $close_tags, $temp, $current_tag;
if ($format = $close_tags[$name]){
switch($name){
case 'header':
return_page($temp);
$temp = '';
break;
default:
break;
}
}
}
// this function is passed data between elements
// theu $data would equal 'Title Here'
// in the line <TITLE>Title Here</TITLE>
function characterData($parser, $data){
global $current_tag, $temp, $catID;
switch($current_tag){
case 'TITLE':
$temp['title'] = $data;
$current_tag = '';
break;
case 'URL':
$temp['url'] = $data;
$current_tag = '';
break;
default:
break;
}
}
?>
<?php
function return_page(){
global $temp;
echo 'o <A HREF="'.$temp['url'].'">'.$temp['title'].'</A><BR>';
}
// what are we parsing?
$xml_file = 'tester.xml';
// declare the character set - UTF-8 is the default
$type = 'UTF-8';
// create our parser
$xml_parser = xml_parser_create($type);
// set some parser options
xml_parser_set_option($xml_parser, XML_OPTION_CASE_FOLDING, true);
xml_parser_set_option($xml_parser, XML_OPTION_TARGET_ENCODING, 'UTF-8');
// this tells PHP what functions to call when it finds an element
// these funcitons also handle the element's attributes
xml_set_element_handler($xml_parser, 'startElement','endElement');
// this tells PHP what function to use on the character data
xml_set_character_data_handler($xml_parser, 'characterData');
if (!($fp = fopen($xml_file, 'r'))) {
die("Could not open $xml_file for parsing!\n");
}
// loop through the file and parse baby!
while ($data = fread($fp, 4096)) {
if (!($data = utf8_encode($data))) {
echo 'ERROR'."\n";
}
if (!xml_parse($xml_parser, $data, feof($fp))) {
die(sprintf( "XML error: %s at line %d\n\n",
xml_error_string(xml_get_error_code($xml_parser)),
xml_get_current_line_number($xml_parser)));
}
}
xml_parser_free($xml_parser);
?>