Code has been added to clipboard!
Initializing Expat XML Parser
Example
<?php
// Initializing the XML Expat parser
$expat_parser = xml_parser_create();
// Declare the function we will use at the beginning of the element
function start($expat_parser,$_name,$_attrs)Â { Â Â
switch($_name)Â { Â Â Â Â
case "NOTE":
echo "-- Note --<br>"; Â Â Â Â
break; Â Â Â Â
case "TO":
echo "Recipient: "; Â Â Â Â
break; Â Â Â Â
case "FROM":
echo "Sender: "; Â Â Â Â
break; Â Â Â Â
case "HEADING":
echo "Topic: "; Â Â Â Â
break; Â Â Â Â
case "BODY":
echo "Letter: ";
}
}
//Declare the function we will use at the ending of the element
function stop($expat_parser, $_name)Â { Â Â
echo "<br>";
}
//Declare the function we will use for finding character data
function char($expat_parser, $data)Â {
echo $data;
}
// Declare the handler for our elements
xml_set_element_handler($expat_parser, "start", "stop");
// Declare the handler for our data
xml_set_character_data_handler($expat_parser, "char");
// Open the XML file we want to read
$fp = fopen("node.xml", "r");
// Read our data
while ($data=fread($fp,4096))Â {
xml_parse($expat_parser, $data, feof($fp)) or die (sprintf("XML Error: %s at line %d", xml_error_string(xml_get_error_code($expat_parser)), xml_get_current_line_number($expat_parser)));
}
// Free/stop using the XML Expat Parser
xml_parser_free($expat_parser);
?>