Extensible Markup Language (XML) is a markup language that defines a set of rules for encoding documents in a format which is both human-readable and machine-readable. It is defined by the W3C’s XML 1.0 Specification and by several other related specifications. This program below parses an XML file and show its contents on the page. It assumes that xml file name is example.xml file. This code sample uses XML parser to traverse an XML file.

<?php
function endHandler($parser, $name)
{
	switch($name)
	{
		case 'EXAMPLE':
			print("\n");
			break;
			case 'TITLE';
			print("");
			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");

?>
Tagged with: PHPProgrammingSource Code
 

Leave a Reply

Your email address will not be published.

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>

 

Looking for something?

Use the form below to search the site:


Still not finding what you're looking for? Drop a comment on a post or contact us so we can take care of it!

Related News Feeds

Set your Twitter account name in your settings to use the TwitterBar Section.