How to parse the contents of an XML file using PHP?

Parse XML using PHP

Parse XML using PHP

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.

<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>
Categories: PHP Source Code
M. Saqib: Saqib is Master-level Senior Software Engineer with over 14 years of experience in designing and developing large-scale software and web applications. He has more than eight years experience of leading software development teams. Saqib provides consultancy to develop software systems and web services for Fortune 500 companies. He has hands-on experience in C/C++ Java, JavaScript, PHP and .NET Technologies. Saqib owns and write contents on mycplus.com since 2004.
Related Post