extracting data from XML file to PHP variable ..
| From: | Kenn Murrah | Date: | Wed, 31 Mar 2004 20:46:07 +0000 |
| Subject: | extracting data from XML file to PHP variable .. | ||
| Groups: | php.general | ||
| Request: | Send a blank email to php-general+get-181971@lists.php.net to get a copy of this message | ||
Greetings.
I've been RTFM all morning and for the live of me can't figure out how
to relate the element name to its data, e.g. if the element name is
"fullname" and the data is "John Doe' how do I achieve $fullname =
"John Doe" .... I must not be understanding how xml_parse works, and
my searches have turned up nothing.
Any help would be appreciated.
thanks.
function startElement($xml_parser, $name, $attributes) {
print("<p><i>Encountered Start Element For: </i>$name\n");
}
function endElement($xml_parser, $name) {
print("<p><i>Encountered End Element For: <i/>$name\n");
}
function characterData($xml_parser, $data) {
if ($data != "\n") {
$data = ereg_replace ("\:", "&", $data);
print ("<p><i>Encountered Character Data: </i>$data\n");
}
}
function load_data($file) {
$fh = fopen($file, "r") or die ("<p>COULD NOT OPEN FILE!");
$data = fread($fh, filesize($file));
return $data;
}
$xml_parser = xml_parser_create();
xml_set_element_handler ($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
xml_parse($xml_parser, load_data($file)) or die ("<p>ERROR PARSING XML!");
xml_parser_free($xml_parser) ;
------------------------ Yahoo! Groups Sponsor --