YouTip LogoYouTip

Func Xml Set Default Handler

## PHP XML Parser ( XML Expat Parser]( XML DOM Parser]( SimpleXML Tutorial]( ## PHP & XML ( XML Expat Functions]( ## PHP Reference ( Calendar Functions]( Date Functions]( Directory Functions]( Error Functions]( Filesystem Functions]( Filter Functions]( FTP Functions]( HTTP Functions]( Libxml Functions]( Mail Functions]( Math Functions]( Misc Functions]( MySQLi Functions]( ODBC Functions]( SimpleXML Functions]( String Functions]( XML Parser Functions]( Zip File Functions]( ## PHP Interview Questions ( ## PHP Tool ( XML Formatting]( HTML Formatting]( Hex Conversion]( Password Generator]( --- # xml_set_default_handler() **php** ```php <?php //Initialize the XML parser $parser=xml_parser_create(); //Function to use at the start of an element function start($parser,$element_name,$element_attrs) { switch($element_name) { case "NOTE": echo "-- Note --
"; break; case "TO": echo "To: "; break; case "FROM": echo "From: "; break; case "HEAD": echo "Heading: "; break; case "BODY": echo "Message: "; } } //Function to use at the end of an element function stop($parser,$element_name) { echo "
"; } //Function to use when finding character data function char($parser,$data) { echo $data; } //Specify element handler xml_set_element_handler($parser,"start","stop"); //Specify data handler xml_set_character_data_handler($parser,"char"); //Open XML file $fp=fopen("test.xml","r"); //Read data while ($data=fread($fp,4096)) { xml_parse($parser,$data,feof($fp)) or die (sprintf("XML Error: %s at line %d", xml_error_string(xml_get_error_code($parser)), xml_get_current_line_number($parser))); } //Free the XML parser xml_parser_free($parser); ?> ``` **test.xml** ```xml George John Reminder Don't forget the meeting! ``` **Output:** ``` -- Note -- To: George From: John Heading: Reminder Message: Don't forget the meeting! ``` ## Definition and Usage The xml_set_default_handler() function is a parser function that handles the XML file's default data. **Tip:** This function can also handle character data (CDATA). ## Syntax ```php xml_set_default_handler(parser,handler) ``` ## Parameters | Parameter | Description | |-----------|-------------| | parser | Required. Specifies the XML parser to use | | handler | Required. Specifies a function to be called when the parser finds default data | ## Technical Details | Return Value: | Returns TRUE on success, or FALSE on failure | |---------------|---------------------------------------------| ## See Also * [xml_set_element_handler()]( - Sets the element handler function * [xml_set_character_data_handler()]( - Sets the character data handler function --- ## Related Articles * [PHP xml_set_element_handler() function]( * [PHP xml_set_character_data_handler() function]( * [PHP xml_parser_create() function]( * [PHP xml_parse() function]( * [PHP xml_parser_free() function]( ( ( [← Prev: xml_set_character_data_handler()]( [Next: xml_set_element_handler() β†’]( Β© | ( | ( | ( []( [](javascript:;)
← Func Xml Set Element HandlerFunc Xml Set Character Data Ha β†’