Login   Register  
PHP Classes
elePHPant
Icontem

File: test_xml_parser.php

Recommend this page to a friend!
Stumble It! Stumble It! Bookmark in del.icio.us Bookmark in del.icio.us
  Classes of Manuel Lemos  >  Generic XML parser class  >  test_xml_parser.php  >  Download  
File: test_xml_parser.php
Role: Example script
Content type: text/plain
Description: Class test script
Class: Generic XML parser class
Arbitrary XML parser.
Author: By
Last change: Displayed the position when a parsing error occurs.
Reformated the HTML tags and PHP sections.
Date: 1 year ago
Size: 2,129 bytes
 

Contents

Class file image Download
<?php
/*
 * test_xml_parser.php
 *
 * @(#) $Id: test_xml_parser.php,v 1.10 2012/09/05 09:27:07 mlemos Exp $
 *
 */

?><html>
<head>
<title>Test for Manuel Lemos's XML parser PHP class</title>
</head>
<body>
<h1 align="center">Test for Manuel Lemos's XML parser PHP class</h1>
<hr>
<?php
    
require("xml_parser.php");

Function 
DumpArray(&$array,$indent)
{
    for(
Reset($array),$node=0;$node<count($array);Next($array),$node++)
    {
        echo 
$indent."\"".Key($array)."\"=";
        
$value=$array[Key($array)];
        if(
GetType($value)=="array")
        {
            echo 
"\n".$indent."[\n";
            
DumpArray($value,$indent."\t");
            echo 
$indent."]\n";
        }
        else
            echo 
"\"$value\"\n";
    }
}

Function 
DumpStructure(&$structure,&$positions,$path)
{
    echo 
"[".$positions[$path]["Line"].",".$positions[$path]["Column"].",".$positions[$path]["Byte"]."]";
    if(
GetType($structure[$path])=="array")
    {
        echo 
"&lt;".$structure[$path]["Tag"];
        if(IsSet(
$structure[$path]["Attributes"]))
        {
            
$attributes $structure[$path]["Attributes"];
            
$ta count($attributes);
            for(
Reset($attributes), $a 0$a $taNext($attributes), ++$a)
            {
                
$attribute Key($attributes);
                echo 
" "$attribute"=\""HtmlSpecialChars($attributes[$attribute]), "\"";
            }
        }
        echo 
"&gt;";
        for(
$element=0;$element<$structure[$path]["Elements"];$element++)
            
DumpStructure($structure,$positions,$path.",$element");
        echo 
"&lt;/".$structure[$path]["Tag"]."&gt;";
    }
    else
        echo 
$structure[$path];
}

    
$file_name="example.xml";
    
$error=XMLParseFile($parser,$file_name,1,$file_name.".cache");
    if(
strcmp($error,""))
        echo 
"<H2><center>Parser error: ".$error." Line: ".$parser->error_line." Column: ".$parser->error_column." Byte index: ".$parser->error_byte_index."</center></H2>\n";
    else
    {
        echo 
"<H2><center>Parsed file structure</center></H2>\n";
        echo 
"<P>This example dumps the structure of the elements of an XML file by displaying the tags and data preceded by their positions in the file: line number, column number and file byte index.</P>\n";
        echo 
"<PRE>";
        
DumpStructure($parser->structure,$parser->positions,"0");
        echo 
"</PRE>\n";
    }
?></body>
</html>