Here's the code:
PHP Code:
// xml source
$xml_file = "xml/meetings.xml";
// hash of opening HTML tags
$startTags = array(
"MEETINGS" => "",
//...snip...
"AGENDA" => " <a href='agendas/",
"AGENDA_TEXT" => "' title='Word'>"
);
// hash of closing HTML tags
$endTags = array(
"MEETINGS" => "",
//...snip...
"AGENDA" => "",
"AGENDA_TEXT" => "</a></td>"
);
function startElement($parser, $NAME, $attrs) {
global $startTags;
// if tag exists as key, print value
if ($startTags[$NAME]) print $startTags[$NAME];
}
function endElement($parser, $NAME) {
global $endTags;
if ($endTags[$NAME]) print $endTags[$NAME];
}
function characterData($parser, $data) {
print $data;
}
// initialize the parser
$xml_parser = xml_parser_create();
// set callback functions
xml_set_element_handler($xml_parser, "startElement", "endElement");
xml_set_character_data_handler($xml_parser, "characterData");
// open xml file
$fp = fopen($xml_file, "r");
// read and parse data
while ($data = fread($fp, 4096))
{
xml_parse($xml_parser, $data, feof($fp));
}
// close the parser
xml_parser_free($xml_parser);
The XML takes the form:
<meetings>
<agenda>Ag040603.doc</agenda>
<agenda_text>(Agenda)</agenda_text>
</meetings>
It's pretty basic because I am a beginner. There are many more fields in there I have left out for this example. Note that if you open the file in Notepad, there are returns and 9 spaces before the key elements.
Bookmarks