Example of using functions to read XML files in php
Else
Echo"";
}
Function content ($ x, $ content ){
Echo $ content;
}
$ Printerror = false;
$ Xmlfile = "user. xml ";
// Read data in step 2
$ Fp = fopen ($ xmlfile, "r ");
While (! Feof ($ fp )){
$ Data = fread ($ fp, 1024 );
// Start parsing
If (! Xml_parse ($ xml, $ data )){
$ Printerror = true;
}
}
// Close the file
Fclose ($ fp );
If ($ printerror ){
$ Row = xml_get_current_line_number ($ xml );
$ Col = xml_get_current_column_number ($ xml );
$ Errormess = xml_error_string (xml_get_error_code ($ xml ));
Echo "in the file {$ xmlfile}, [{$ row} row, {$ col} column]: {$ errormess }.";
}
// Close the parser
Xml_parser_free ($ xml );
?>
-
// Step 1 create the parser xml_parser_create (encoding );
- $ Xml = xml_parser_create ('utf-8 ');
- // Xml_parser_set_option-set options for specified XML parsing
- // Xml_parser_set_option ($ xml, XML_OPTION_CASE_FOLDING, false); false is output as is
- // Register the event. what functions will be used to start and end table timing?
- Xml_set_element_handler ($ xml, "starttag", "endtag ");
-
Xml_set_character_data_handler ($ xml, "content ");
-
Function starttag ($ x, $ tagName, $ args ){
- If ($ tagName = "USERS ")
- Echo"
- Echo"
- } Else
- Echo"
{$ TagName }";
- Else if ($ tagName = "USER "){
- Echo"
";
| {$ Args ['id']} | ";
";
- }
-
Function endtag ($ x, $ tagName ){
- If ($ tagName = "USERS ")
- Echo"
|
";
- Else if ($ tagName = "USER ")
- Echo"
|
";