turkcedil
> Anasayfa > Makaleler > PHP > PHP Xml-SAX array kullanimi
Web Galeriden
1489 adet Kaliteli Png icon
Attachment
PHP Xml-SAX array kullanimi
Tarih 30/03/2010 16:26  Yazar Hüseyin AKTURAN  Hitler 701  Dil Varsayılan
Php ile Xml Dosyasi icinde bulunan bilgilerini array icine paketlemek (atmak) daha sonra bu paket icinde istenilen bilgilere ulasmak.

 

SAX_array.php

<?php
class SAXParserArray{

private $parser;
private $file;
private $error=FALSE;
private $elements=array();
private $frequency=array();

public function __construct($file,$error=true){

$this->SetFile($file);
$this->parser= xml_parser_create();
xml_parser_set_option($this->parser,XML_OPTION_CASE_FOLDING,FALSE);

}

public function getElements(){

return $this->elements;
}

public function getFrequency(){

return $this->frequency;

}

public function setFile($file){

$this->file=$file;

}

public function setError($error){

$this->error=$error;

}

private function getError($parser){

$error_Code=xml_getError_code($parser);
die("Xml Parser -Fehler (Fehlernr.".$error_Code.")".xml_error_string($error_Code)." bei Zeile: "
.xml_get_current_line_number($parser)." | Spalte;"
.xml_get_current_column_number($parser)." | Byte: "
.xml_get_current_byte_index($parser));
}

public function parse(){

$elements=array();
$frequency=array();

if(!$handle=fopen($this->file,"r")){

die("Datei nicht vorhanden!");
}
// lesen,duchlaufen,verarbeiten
while($data=fread($handle,4096)){

if (!xml_parse_into_struct($this->parser,$data,$this->elements,$this ->frequency)){

if ($this->error){

SAXParserArray::getError($this->parser);

}
else{

$this->getError($this->parser);

}



}
} // while



} //parse()


} //class
?>

SAX_array_test.php

<?php
include('SAX_array.php');

$SAX=new SAXParserArray("tarifliste_elemente.xml",true);
$SAX->parse();

$elements=array();
$elements=$SAX->getElements();
$frequency=array();
$frequency=$SAX->getFrequency;

$output="";

foreach($elements as $XMLTag){

if ($XMLTag["tag"]=="Tarifliste" && $XMLTag["type"]=="open"){

$output.="<ul>
";

}

if ($XMLTag["tag"]=="Tarif" && $XMLTag["type"]=="open"){

$output.="<li>";

}

if ($XMLTag["tag"]=="Name" && $XMLTag["type"]=="complete"){

$output.=$XMLTag["attributes"]["Nr"].": ".$XMLTag["value"]."<br>";

}

   if ($XMLTag["tag"]=="Preis" && $XMLTag["type"]=="complete"){

$output.="<li>".$XMLTag["attributes"]["Nr"].": ".$XMLTag["value"]."</li>";

}

   if ($XMLTag["tag"]=="Tarif" && $XMLTag["type"]=="close"){

$output.="</li>";

}

   if ($XMLTag["tag"]=="Tarifliste" && $XMLTag["type"]=="close"){

$output.="</ul>";

}

}

echo $output."<br>";
?>

tarifliste_elemente.xml

<?xml version="1.0" encoding="UTF-8"?>
<Tarifliste>
<Tarif>
<Name Nr="14">Abendessen</Name>
<Gueltigkeit>
<Datum>
<Von top="15">01.07.03</Von>
<Bis>31.12.03</Bis>
</Datum>
<Uhrzeit>
<Von>15</Von>
<Bis>20</Bis>
</Uhrzeit>
</Gueltigkeit>
<Preis Nr="16">1,5</Preis>
</Tarif>
</Tarifliste>



Yorum Yok.