note 42343 added to ref.xml
| From: | johnt at divector dot net | Date: | Wed, 12 May 2004 17:40:57 +0000 |
| Subject: | note 42343 added to ref.xml | ||
| Groups: | php.notes | ||
| Request: | Send a blank email to php-notes+get-69533@lists.php.net to get a copy of this message | ||
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>
----
Manual Page -- http://www.php.net/manual/en/ref.xml.php
Edit -- http://master.php.net/manage/user-notes.php?action=edit+42343
Delete -- http://master.php.net/manage/user-notes.php?action=delete+42343&report=yes
Reject -- http://master.php.net/manage/user-notes.php?action=reject+42343&report=yes
Search -- http://master.php.net/manage/user-notes.php