<?php /* * extract_valid_xml_data.php * * @(#) $Id: extract_valid_xml_data.php,v 1.2 2012/09/04 10:08:03 mlemos Exp $ * */
?><html> <head> <title>Validate and extract data from XML document</title> </head> <body> <h1 align="center">Validate and extract data from XML document</h1> <hr> <?php require('xml_parser.php');
$file_name = 'example.xml'; $error = XMLParseFile($parser, $file_name, 1, $file_name.'.cache'); if(strlen($error) == 0) { /* * The types array defines the types and structure of the document * to be validated before extracting its data values. */ $content_types = array( 'CONTENTS'=>array( 'type'=>'hash', 'types'=>array( 'OBJECT'=>array( 'type'=>'hash', /* allow tag to appear an unlimited number of times */ 'maximum'=>'*', 'types'=>array( 'NAME'=>array( /* The default maximum and minimum times is 1 for required tags */ 'type'=>'text', ), 'CLASS'=>array( 'type'=>'text', ), 'ATTRIBUTES'=>array( 'type'=>'path', /* make it optional by not requiring a minimum number of times to appear */ 'minimum'=>0, ), ) ) ) ) ); $root_path = ''; $root_element = 'CONTENTS'; $hash = 1; $error = $parser->ExtractElementData($root_path, $root_element, $content_types, $hash, $contents); }
if(strlen($error)) echo '<h2 align="center">Parser error: ', $error, '</h2>'; else { echo '<h2 align="center">Extracted data structure</h2>'; echo '<p>This example dumps the structure and values of the XML file.</p>'; echo '<pre>'; /* * The resulting associative array has the tag names and values * that were found and validated in the XML document */ var_dump($contents); echo '</pre>'; } ?></body> </html>
|