Parsing XML with PHP
Parsing XML with PHP is not a simple as it should be. The following method requires a little abstract thinking. The parsing is events based, meaning, whenever a new tag is encountered, an event is raised. You respond to the event with your own code. The following example, which parses dictionary terms and their meanings, might help make sense of things:
<?
$file = "src/xmlsrc.xml";
$term = $_REQUEST['term'];
$definition = "";
$type = "";
$getdef = 0;
function xml_parse_from_file($parser, $file)
{
if(!file_exists($file))
{
die("Can't find file \"$file\".");
}
if(!($fp = @fopen($file, "r")))
{
die("Can't open file \"$file\".");
}
while($data = fread($fp, 4096))
{
if(!xml_parse($parser, $data, feof($fp)))
{
return(false);
}
}
fclose($fp);
return(true);
}
function start_element($parser, $name, $attrs)
{
global $type;
if ($name == 'word')
$type = "Word";
elseif ($name == 'definition')
$type = "Definition";
else
$type = "";
}
function stop_element($parser, $name)
{
}
function char_data($parser, $data)
{
global $definition, $type, $getdef, $term;
if ($type == 'Word' && trim($data) == trim($term))
{
$getdef = 1;
}
if ($getdef == 2)
{
$definition = $data;
$getdef = 0;
}
if ($type == 'Definition' && $getdef == 1)
{
$getdef = 2;
}
}
// Create Expat parser
$parser = xml_parser_create();
// Set handler functions
xml_set_element_handler($parser, "start_element", "stop_element");
xml_set_character_data_handler($parser, "char_data");
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
// Parse the file
$ret = xml_parse_from_file($parser, $file);
if(!$ret)
{
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($parser)),
xml_get_current_line_number($parser)));
}
// Free parser
xml_parser_free($parser);
?>
<?
$file = "src/xmlsrc.xml";
$term = $_REQUEST['term'];
$definition = "";
$type = "";
$getdef = 0;
function xml_parse_from_file($parser, $file)
{
if(!file_exists($file))
{
die("Can't find file \"$file\".");
}
if(!($fp = @fopen($file, "r")))
{
die("Can't open file \"$file\".");
}
while($data = fread($fp, 4096))
{
if(!xml_parse($parser, $data, feof($fp)))
{
return(false);
}
}
fclose($fp);
return(true);
}
function start_element($parser, $name, $attrs)
{
global $type;
if ($name == 'word')
$type = "Word";
elseif ($name == 'definition')
$type = "Definition";
else
$type = "";
}
function stop_element($parser, $name)
{
}
function char_data($parser, $data)
{
global $definition, $type, $getdef, $term;
if ($type == 'Word' && trim($data) == trim($term))
{
$getdef = 1;
}
if ($getdef == 2)
{
$definition = $data;
$getdef = 0;
}
if ($type == 'Definition' && $getdef == 1)
{
$getdef = 2;
}
}
// Create Expat parser
$parser = xml_parser_create();
// Set handler functions
xml_set_element_handler($parser, "start_element", "stop_element");
xml_set_character_data_handler($parser, "char_data");
xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
// Parse the file
$ret = xml_parse_from_file($parser, $file);
if(!$ret)
{
die(sprintf("XML error: %s at line %d",
xml_error_string(xml_get_error_code($parser)),
xml_get_current_line_number($parser)));
}
// Free parser
xml_parser_free($parser);
?>
Need assistance with your project? Universal Web Services can help.
Contact us to request a quote.