Web/PHP
xml_parser
aucd29
2013. 9. 26. 21:47
When I first read this documentation, and tried the examples none of which seemed to work. The contributed ones in the notes were a bit "long winded" for a simple example to demonstrate a working example of the required functions. So I came up with this, while not perfect, it does execute and give an idea of the functions and Call back functions to create an XML Parser.
<?PHP
// Variables
$file = "xmldata.xml";
$feed = array();
$key = "";
$info = "";
$in_HEAD = false;
function startElement($xml_parser, $name, $attrs ) {
global $feed, $key, $in_HEAD;
$key = $name;
if( $name == "HEAD" )
$in_HEAD = true; }
function endElement($xml_parser, $name) {
// The Workhorse of the Call Back Functions
// Most of the programming will be put in this function.
global $feed, $key, $info, $in_HEAD;
if( $name == "HEAD" )
$in_HEAD = false;
if($in_HEAD==false)
$key = $name;
elseif( $in_HEAD )
$key = "HEAD_".$name;
$feed[$key] = $info;
$info = ""; }
function charData($xml_parser, $data ) {
// $xml_parser - The resource ID for this parser
// $data - The character data returned by the parser, from the XML file
global $info;
$info .= $data; }
// The Beginning of Execution *******************************************
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "charData" );
$fp = fopen($file, "r");
while ($data = fread($fp, 8192))
!xml_parse($xml_parser, $data, feof($fp));
xml_parser_free($xml_parser);
// Start Web page
echo "<HTML>\n";
echo "<HEAD>\n";
echo "<TITLE>".$feed['HEAD_TITLE']."</TITLE>\n";
echo "</HEAD>\n";
echo "<BODY>\n";
echo "<CENTER><H1>".$feed['HEAD_TITLE']."</H1></CENTER>\n";
echo "<HR>\n";
foreach( $feed as $assoc_index => $value )
{
echo "\$assoc_index = $assoc_index<BR> \$value = $value<BR><BR>\n";
}
echo "</BODY>\n";
echo "</HTML>\n";
?>
<?xml version="1.0" encoding="UTF-8"?>
<XML>
<HEAD>
<TITLE>XML Data Demo</TITLE>
<DESCRIPTION>XML Data Demo for testing XML parsers. A Simple demo for demonstrating the PHP Call Back functions.</DESCRIPTION>
</HEAD>
<FUNCLIST>The functions necessary for Parser Creation are: xml_parser_create(); xml_set_element_handler($xml_parser, "startElement", "endElement");xml_set_character_data_handler($xml_parser, "charData" );</FUNCLIST>
<RECAP>The Really neat thing here is allowing the programmer complete control over these call back functions to parse virtually any XML file. In my opinion, an extra variable in the Call Back functions allowing an array to be passed would be better, this would keep globals from being used.</RECAP>
</XML>
<?PHP
// Variables
$file = "xmldata.xml";
$feed = array();
$key = "";
$info = "";
$in_HEAD = false;
function startElement($xml_parser, $name, $attrs ) {
global $feed, $key, $in_HEAD;
$key = $name;
if( $name == "HEAD" )
$in_HEAD = true; }
function endElement($xml_parser, $name) {
// The Workhorse of the Call Back Functions
// Most of the programming will be put in this function.
global $feed, $key, $info, $in_HEAD;
if( $name == "HEAD" )
$in_HEAD = false;
if($in_HEAD==false)
$key = $name;
elseif( $in_HEAD )
$key = "HEAD_".$name;
$feed[$key] = $info;
$info = ""; }
function charData($xml_parser, $data ) {
// $xml_parser - The resource ID for this parser
// $data - The character data returned by the parser, from the XML file
global $info;
$info .= $data; }
// The Beginning of Execution *******************************************
$xml_parser = xml_parser_create();
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "charData" );
$fp = fopen($file, "r");
while ($data = fread($fp, 8192))
!xml_parse($xml_parser, $data, feof($fp));
xml_parser_free($xml_parser);
// Start Web page
echo "<HTML>\n";
echo "<HEAD>\n";
echo "<TITLE>".$feed['HEAD_TITLE']."</TITLE>\n";
echo "</HEAD>\n";
echo "<BODY>\n";
echo "<CENTER><H1>".$feed['HEAD_TITLE']."</H1></CENTER>\n";
echo "<HR>\n";
foreach( $feed as $assoc_index => $value )
{
echo "\$assoc_index = $assoc_index<BR> \$value = $value<BR><BR>\n";
}
echo "</BODY>\n";
echo "</HTML>\n";
?>
<?xml version="1.0" encoding="UTF-8"?>
<XML>
<HEAD>
<TITLE>XML Data Demo</TITLE>
<DESCRIPTION>XML Data Demo for testing XML parsers. A Simple demo for demonstrating the PHP Call Back functions.</DESCRIPTION>
</HEAD>
<FUNCLIST>The functions necessary for Parser Creation are: xml_parser_create(); xml_set_element_handler($xml_parser, "startElement", "endElement");xml_set_character_data_handler($xml_parser, "charData" );</FUNCLIST>
<RECAP>The Really neat thing here is allowing the programmer complete control over these call back functions to parse virtually any XML file. In my opinion, an extra variable in the Call Back functions allowing an array to be passed would be better, this would keep globals from being used.</RECAP>
</XML>