Zend - The PHP Company




XML

Add Code


XML easy parser  

Type: class
Added by: shadowman
Entered: 07/12/2000
Last modified: 04/12/2000
Rating: **** (7 votes)
Views: 13461
English:
This class:
            * it works with any document XML storages in file or variable;
             * It recovers data of any element or attribute;
             * It indicates which elements constitute the document XML;
             * It indicates which attributes belong to an element;
             * it treats the mistakes exhibiting the line and the column where is the mistake highlighting;
             * works with external entities.

Portugu�s:
Esta classe:
            * Trabalha com qualquer documento XML aramazenado em arquivo ou vari�vel;
             * Recupera dados de qualquer elemento ou atributo;
             * Indica quais elementos constituem o documento XML;
             * Indica quais atributos pertencem a um elemento;
             * Trata os erros exibindo a linha e destacando a coluna onde est� o erro;
             * Trabalha com entities externas.



<?

class easy_parser{

var 
$old_element "";
var 
$attribute = array();
var 
$dado "";
var 
$array_element = array();
var 
$array_attribute_value = array();
var 
$array_attributes = array();
var 
$array_nodes = array();
var 
$current_element "";
var 
$array_texto = array();
var 
$get_err "";
var 
$texto "";

function 
getpart($texto,$string,$reset=TRUE,$position=0)
{
  static 
$p0=0;
  
$p0 += $position;
  if(
$reset){
    
$p0 0;
  }
  
$get "";
  
$string trim($string);
  if(
strpos($texto,"<$string>",$p0) >=0)
  {
    
$p1 =  strpos($texto,"<$string>",$p0);
    
$p2 strpos($texto,"</$string>",$p0);
    
$len strlen("<$string>");
    
$get substr($texto,($p1 $len),($p2 $p1 $len));
    
$p0 $p2+$len;
  }
  return 
$get;
}


function 
getrpart($texto,$string)
{
 
  
$get "";
  
$string trim($string);

  if(
strrpos($texto,"<$string>") >=0)
  {

    
$p1 =  strrpos($texto,"<$string>");
    
$texto2 substr($texto,0,$p1);
    
$p2 strrpos($texto2,"</$string>");
    
$len strlen("<$string>");
    
$get substr($texto2,$p2);
  }
  return 
$get;
}

function 
parser($document,$is_file=TRUE){
    
$result TRUE;
    
$parser xml_parser_create();
    
xml_set_object($parser,&$this);    
    
xml_parser_set_option($parser,XML_OPTION_CASE_FOLDING0);
    
xml_set_element_handler($parser,"start_element","end_element");
    
xml_set_character_data_handler($parser,"character_data");

    if(
$is_file){
      if(
file_exists($document)){
        
         
$xml_file fopen($document,"r");
         
$data "";
         
$ind 0;
        while (!
feof($xml_file)) {
           
$data fgets($xml_file,256);
           if(
ereg("<!ENTITY.*SYSTEM",$data,$matchs) && strpos($data,"NDATA") < 1){
            
$arrai split(" ",$data);
            
$arrai[3] = str_replace("">","",$arrai[3]);
            
$arrai[3] = trim(str_replace(""","",$arrai[3]));
            if(
file_exists($arrai[3])){
                
$fp fopen($arrai[3],"r");
                while(!
feof($fp)){
                    
$ext_val[$ind] .= fgets($fp,1024);
                }
                
fclose($fp);
                
$ext_key[$ind] = trim($arrai[1]);
                ++
$ind;
            }
           }

           
$this->texto .= $data;
        }
        
        
fclose($xml_file);
      }else{
        print 
"<b><h3>The file: <font color="blue">$document</font> not found.</b><h3>";
      }
    }else{
      
$this->texto $document;    
     }

    if(isset(
$ext_val)){
      
$ind 0;
      foreach(
$ext_val as $value){
        
$this->texto str_replace("&".$ext_key[$ind].";","$valuen",$this->texto);
        ++
$ind;
      }
    }
    
$this->array_texto split("n",$this->texto);

     if (!
xml_parse($parser$this->texto,TRUE)) {
        
$err_line $this->array_texto[(xml_get_current_line_number($parser)-1)];
        
$err_col xml_get_current_column_number($parser);
        
$text1 "<font color="blue" size="3" face="arial">".htmlentities(substr($err_line,0,($err_col -1)))."</font><font color="red" size="3" face="arial">";
        
$text2 htmlentities(substr($err_line,($err_col),1))."</font><font color="blue" size="3" face="arial">";
        
$text3 htmlentities(substr($err_line,($err_col +1)))."<font>";
        
$this->get_err "<font color="black" size="3" face="arial">Fonte do documento: $document<br>"
        
."<b>XML error: </b>".xml_error_string(xml_get_error_code($parser))." <b>at</b> line "
        
.xml_get_current_line_number($parser)." and colunm $err_col<br>"
        
."<b> Texto: </b>$text1$text2$text3";
        
$result FALSE;
    }

    
xml_parser_free($parser);
    return 
$result;
}

function 
start_element($parser,$element_name,$attributes){
  
$this->attribute $attributes;
  
$this->dado "";
  
$this->current_element $element_name;
  
  if(!
in_array($element_name,$this->array_nodes)){
    
$this->array_nodes[] = $element_name;
  }
   
  while(list(
$key,$value) = each($attributes)){
      
$attr[] = $key;
    }
 
$this->array_attributes[$this->current_element] = $attr;

}

function 
end_element($parser,$element_name){
  if(
trim($this->dado) != ""){
    
$this->array_element[$this->current_element][] = $this->dado;
  }
  
reset($this->attribute);
  
$this->dado "";
  if(
count($this->attribute)>0){
      
$this->array_attribute_value[$this->current_element][] = $this->attribute;
  }
}


function 
character_data($parser,$data){
  
$this->dado .= $data;
   
 }


function 
get_element_value($element,$ind 0){
    return 
$this->array_element[$element][$ind];
}



function 
get_element_rows($element){
    return 
count($this->array_element[$element]);
}



function 
get_elements(){
    return 
$this->array_nodes;
}


function 
get_element_attribute($element,$ind 0,$attribute){
    return 
$this->array_attribute_value[$element][$ind][$attribute];
}



function 
get_attributes($element){
    return 
$this->array_attributes[$element];
}

function 
view_source(){
    
$retorno htmlentities($this->texto);
    return  
str_replace("&gt;","&gt;<br>",$retorno);
}

}

?>


Usage Example


<?
//Exemplo:

//Example:

include("./easy_parser.inc"); // file of easy_parser class

$xml = new easy_parser;
$result $xml->parser("file.xml",true); // if source of document is not a file then use $result = $xml->parser($var_source,false)
print $xml->view_source();
if(
$result){
$elements $xml->get_elements();

print 
"<b><font face="arial" size="3" color="blue"><br>Element list:<p></font>";

while(list(
$key,$value) = each($elements)){
  print 
"&nbsp;&nbsp;$value<br>";
}

print 
"<p><font face="arial" size="3" color="blue">Element value: <p></font>";

print 
"&nbsp;&nbsp;" $xml->get_element_value("file",1) . "<p>";

print 
"<p><font face="arial" size="3" color="blue">Element rows: <p></font>";

print 
"&nbsp;&nbsp;" $xml->get_element_rows("file") . "<p>";

print 
"<p><font face="arial" size="3" color="blue">Element attributes: <p></font>";

$attr $xml->get_attributes("file");
while(list(
$key,$value) = each($attr)){
  print 
"&nbsp;&nbsp;" $value "<br>";
}

print 
"<p><font face="arial" size="3" color="blue">Value of attribute size: <p></font>";

print 
"&nbsp;&nbsp;" $xml->get_element_attribute("file",0,"size") . "<p>";
}else{
print 
$xml->get_err;
}
?>
Source of file.xml:

<?xml version="1.0" encoding="ISO-8859-1"?>
<!DOCTYPE document [
<!ELEMENT document (file+,obs+,dia)+>
<!ELEMENT file (#PCDATA)>
<!ELEMENT obs (#PCDATA)>
<!ELEMENT name (#PCDATA)>
<!ELEMENT age (#PCDATA)>
<!ATTLIST file
  number CDATA #REQUIRED
  size   CDATA #REQUIRED
>
<!ATTLIST obs 
  data CDATA #REQUIRED
>
<!ENTITY data1 "04/12/2000">
<!ENTITY hora1 "10:08:20">
<!ENTITY data2 "05/12/2000">
<!ENTITY hora2 "16:20:30">
<!ENTITY ext1 SYSTEM "file2.xml">
<!ENTITY ext2 SYSTEM "file3.xml">
]>
<document>
<file number="1" size="20">Este � o file numero 1. A data � &data1; e a hora � &hora1;</file>
<obs data="06/12/2000">chato!</obs>
<file number="2" size="25">Este � o file numero 2. A data � &data2; e a hora � &hora2;</file>
<file number="3" size="30"><![CDATA[ oi man�!!!!]]></file>
<obs data="06/12/2000">chato!</obs>
&ext1;&ext2;
</document>

Source of file2.xml:

<age>24</age>
<age>25</age>

Source of file3.xml:

<name>Ze Mane</name>
<name>Rubens Barroso</name>



Rate This Script





Search



This Category All Categories