M
Mechphisto
I'm trying to write a PHP script that will grab the info from an XML
file, and parse and sort the data into their own variables.
Can someone point me a direction for information?
Here's what I have so far. The following script will grab ALL data in
the file and simply make each one bold and separated by a break.
I want to be able to dump the contents of message1 into say $data1 and
message2 into $data2, etc.
Does that make sense?
Thanks for any assistance!
-Liam
<?php
$file = "xml_test.xml";
unset($xml_data);
function contents($parser, $data){
global $xml_data;
$xml_data .= $data;
return $xml_data;
}
function startTag($parser, $data){
global $xml_data;
$xml_data .= "<b>";
return $xml_data;
}
function endTag($parser, $data){
global $xml_data;
$xml_data .= "</b><br />";
return $xml_data;
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startTag", "endTag");
xml_set_character_data_handler($xml_parser, "contents");
$fp = fopen($file, "r");
$data = fread($fp, 80000);
if(!(xml_parse($xml_parser, $data, feof($fp)))){
die("Error on line " . xml_get_current_line_number($xml_parser));
}
xml_parser_free($xml_parser);
fclose($fp);
echo $xml_data;
?>
xml_test.xml contains:
<?xml version="1.0" encoding="utf-8"?>
<text>
<message1>Heres XML text.</message1>
<message2>Heres some more XML text.</message2>
</text>
file, and parse and sort the data into their own variables.
Can someone point me a direction for information?
Here's what I have so far. The following script will grab ALL data in
the file and simply make each one bold and separated by a break.
I want to be able to dump the contents of message1 into say $data1 and
message2 into $data2, etc.
Does that make sense?
Thanks for any assistance!
-Liam
<?php
$file = "xml_test.xml";
unset($xml_data);
function contents($parser, $data){
global $xml_data;
$xml_data .= $data;
return $xml_data;
}
function startTag($parser, $data){
global $xml_data;
$xml_data .= "<b>";
return $xml_data;
}
function endTag($parser, $data){
global $xml_data;
$xml_data .= "</b><br />";
return $xml_data;
}
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startTag", "endTag");
xml_set_character_data_handler($xml_parser, "contents");
$fp = fopen($file, "r");
$data = fread($fp, 80000);
if(!(xml_parse($xml_parser, $data, feof($fp)))){
die("Error on line " . xml_get_current_line_number($xml_parser));
}
xml_parser_free($xml_parser);
fclose($fp);
echo $xml_data;
?>
xml_test.xml contains:
<?xml version="1.0" encoding="utf-8"?>
<text>
<message1>Heres XML text.</message1>
<message2>Heres some more XML text.</message2>
</text>