<!--
<contact id="43956">
<personal>
<name>
<first>J</first>
<middle>J</middle>
<last>J</last>
</name>
<title>Manager</title>
<employer>National</employer>
<dob>1971-12-22</dob>
</personal>
</contact>
-->
<html>
<body>
<?php
$cdata = '';
$contact_id = -1;
$contacts = null;
function start_elem ($parser, $name, $attrs) {
global $contact_id;
if ($name == 'contact') {
$contact_id = $attrs['id'];
$contacts[$contact_id] = array();
}
}
function end_elem ($parser, $name) {
global $cdata, $contacts, $contact_id;
switch ($name)
{
case ("first"):
$contacts[$contact_id]['name']['first'] = $cdata;
break;
case ("last"):
$contacts[$contact_id]['name']['last'] = $cdata;
break;
case ("middle"):
$contacts[$contact_id]['name']['middle'] = $cdata;
break;
case ("title"):
$contacts[$contact_id]['title'] = $cdata;
break;
case ("employer"):
$contacts[$contact_id]['employer'] = $cdata;
break;
case ("dob"):
$contacts[$contact_id]['dob'] = $cdata;
break;
}
$cdata = '';
}
function cdata ($parser, $data) {
global $cdata;
$cdata .= trim ($data);
}
$file = "contact.xml";
$parser = xml_parser_create ();
xml_parser_set_option ($parser, XML_OPTION_CASE_FOLDING, false);
xml_set_element_handler ($parser, "start_elem", "end_elem");
xml_set_character_data_handler ($parser, "cdata");
$fp = fopen ($file, "r");
if (!$fp){
die ("Couldn't open $file for reading");
}
while ($xml_input = fread ($fp, 2048)) {
$ok = xml_parse ($parser, $xml_input, feof ($fp));
if (!$ok){
die (sprintf ("Error in $file: '%s' at line %d",
xml_error_string (xml_get_error_code ($parser)),
xml_get_current_line_number ($parser)));
}
}
xml_parser_free ($parser);
print ("<pre>\n");
print_r ($contacts);
print ("</pre>\n");
?>
</body>
</html>
|