This is simple PHP script to show how to parse the the contents of an XML file and display them. The script uses PHP XML parser to traverse through the xml file and print it’s contents. This extension requires the libxml PHP extension to be enabled.
By default, the script assumes that example.xml file is in the same directory as the script is.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 | <html> <head> <title>Print XML File Data</title> </head> <body> <? /* ** define functions */ function cdataHandler($parser, $data) { print($data); } function startHandler($parser, $name, $attributes) { switch($name) { case 'EXAMPLE': print("<hr />\n"); break; case 'TITLE': print("<b>"); break; case 'CODE': print("<pre>"); break; default: //ignore other tags } } function endHandler($parser, $name) { switch($name) { case 'EXAMPLE': print("<hr />\n"); break; case 'TITLE'; print("</b>"); break; case 'CODE': print(""); break; default: //ignore other tags } } function piHandler($parser, $target, $data) { if($target == "php") { eval($data); } else { print(htmlentities($data)); } } function defaultHandler($parser, $data) { global $defaultText; $defaultText .= $data; } function ndataHandler($parser, $name, $base, $systemID, $publicID, $notation) { print("\n"); } function notationHandler($parser, $name, $base, $systemID, $publicID) { print("\n"); } function externalHandler($parser, $name, $base, $systemID, $publicID) { //here you could create another parser print("\n"); return(TRUE); } /* ** Initialize */ //create parser if(!($parser = xml_parser_create())) { print("Could not create parser! \n"); exit(); } //make sure we’re using ISO-8859-1 encoding if(xml_parser_get_option($parser, XML_OPTION_TARGET_ENCODING) != "ISO-8859-1") { xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "ISO-8859-1"); } //register handlers xml_set_character_data_handler($parser, "cdataHandler"); xml_set_element_handler($parser, "startHandler", "endHandler"); xml_set_processing_instruction_handler($parser, "piHandler"); xml_set_default_handler($parser, "defaultHandler"); xml_set_unparsed_entity_decl_handler($parser, "ndataHandler"); xml_set_notation_decl_handler($parser, "notationHandler"); xml_set_external_entity_ref_handler($parser, "externalHandler"); /* ** Parse file */ if(!($fp = fopen("example.xml", "r"))) { print("Couldn’t open example.xml! \n"); xml_parser_free($parser); exit(); } while($line = fread($fp, 1024)) { if(!xml_parse($parser, $line, feof($fp))) { //Error, so print full info print("ERROR: " . xml_error_string(xml_get_error_code($parser)) . " at line " . xml_get_current_line_number($parser) . ", column " . xml_get_current_column_number($parser) . ", byte " . xml_get_current_byte_index($parser) . " \n"); } } //destroy parser xml_parser_free($parser); print("Text handled by the default handler:\n"); print("" . htmlentities($defaultText) . "\n"); ?> </body> </html> |