Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: XML + PHP => HTML ...
Forum PHP.pl > Forum > PHP
GeoS
Mam pytanko do szanownych forumowiczow: czy trafiliscie w swojej karierze programisty na jakas zgrabna klase php do obslugi danych w XMLu :?:
Chodzi mi dokladnie o klase, ktora bylaby szybka, latwa, przyjemna i tworzyla z dokumentu XML tablice danych w php.

Bede wdzieczny za wszelkie linki, fragmenty kodu.

PS Dzisiaj poswiecilem na szukanie tego rozwiazania caly wieczor, ale widac to malo. W akcie desperecji postanowilem sie zglosic po pomoc do was smile.gif Tylko nie odsylajcie mnie do Zenda, google, hotscripts, webdeveloper.com, webdeveloper.pl, ... aaevil.gif
Seth
Dogrzebalem sie do pewnych class oto one:
Kod
<?php



/*

        (c) 2000 Hans Anderson Corporation.  All Rights Reserved.

        You are free to use and modify this class under the same

        guidelines found in the php License.



        -----------



        bugs/me:

        http://www.hansanderson.com/php/

        me@hansanderson.com



        -----------



        YOU NEED PHP4 FOR THIS -- even if you have XML support in PHP3

        (sorry, I hate that, too, but I use some other PHP4 functionality)



        -----------



        What class.xml.php is:



        A very, very easy to use XML parser class. It uses php's XML functions

        for you, returning all the objects in the XML file in an easy to use

        Tree.  It also can be used as a statistics-type program, as you can

        use the size() method to find out how many of each elements there are.



        -----------



        Sample use:



        require('class.xml.php');

        $file = "somexmldata.xml";

        $data = implode("",file($file)) or die("could not open XML input file");



        $xml = new xml($data);



        for($i=0;$i<$xml_item_url->size();$i++) {

         print $xml_item_url->get($i) . "nn";

        }



        (that's it! slick, huh?)

        -----------



        Two ways to call:



                $xml = new xml($data);

                - or -

                $xml = new xml($data,"jellyfish");



        The second argument (jellyfish) is optional.  Default is 'xml'.



        if you have



        "<html><head><title>Title stuff</title></head></html>"



        the object you get would look like this:



        $xml_html_head_title (value is "Title stuff")



        but if you created the object like

                $xml = new xml($data,"jellyfish");



        then your object would look like this:



        $jellyfish_html_head_title (value is "Title stuff")



        Comprende?  It's only really needed if you want something else to

        start off your object names.  If you don't understand this, just

        call it like $xml = new xml($data);



        ----------



        Class Methods You Will Use:



        where 'classname' is the name of an XML object, like xml_head_title



        classname->get($idx);

         - returns array item found at $idx



        classname->size();

         - returns the size of the storage array



        -----------



        Explanation:



        This class takes valid XML data as an argument and

        returns all the information in a concise little

        tree of objects.



        Here's how it works:



                Data:



                        <html>

                         <head>

                          <title>Hans Anderson's XML Class</title>

                         </head>

                         <body>

                         </body>

                        </html>



                Run the data through my class, then access the title like this:

                $xml_html_head_title->get(0);



                That is an object, but you don't have to worry about

                contructing it.  The data is stored in an array in the

                object, so the ->get(0) is a way to get the data out.  If

                there were more than one item, you'd do a loop just like

                any array:



                for($i=0;$i<$xml_html_head_title->size();$i++) {

                        print $xml_html_head_title->get($i) . "n";

                }



                Yes, the variable names *are* long, but it's the best

                way to create the tree, IMO.



                -----------

                Errors:



                if you get the error "Fatal error: Call to a member function on a non-object",

                you have misspelled the name of the XML tags, or you have the case wrong.



*/



class stor {



        var $a; // holds data



                function add($data) {

                        $this->a[] = $data;

                }



                function get($idx) {

                        return $this->a[$idx];

                }



                function size() {

                        return sizeof($this->a);

                }



} // end class stor;





class xml {



        // initialize some variables

        var $identifier='xml';

        var $current_tag='';

        var $separator='_x_';

        var $delimiter='_';

        var $xml_parser;



        /* Here are the XML functions needed by expat */



        function startElement($parser, $name, $attrs) {

                // global current tag, separator, tagdata?

                $this->current_tag .= $this->separator . $name;

                if(count($attrs)>0) $this->tagdata = $attrs;

        }



        function endElement($parser, $name) {

                //global $current_tag, $tagdata, $separator ?

                $curtag = $this->identifier . str_replace($this->separator,$this->delimiter,$this->current_tag);



                if(!is_array($this->tagdata)) $this->tagdata = trim($this->tagdata);



                if($this->tagdata) {

                        

                        if(is_object($GLOBALS["$curtag"])) {

                                $GLOBALS["$curtag"]->add($this->tagdata);

                        } else {

                                $GLOBALS["$curtag"] = new stor;

                                $GLOBALS["$curtag"]->add($this->tagdata);

                        }



                } else {



                        // if there is no cdata, we still stor something

                        // in the array, so we can count the levels later



                        if(is_object($GLOBALS["$curtag"])) {

                                $GLOBALS["$curtag"]->add(FALSE);

                        } else {

                                $GLOBALS["$curtag"] = new stor;

                                $GLOBALS["$curtag"]->add(FALSE);

                        }



                }



                // overwrite curtag, no longer needing the old one

                $curtag = strrev($this->current_tag);

                $curtag = substr($curtag,strpos($curtag,$this->separator)+strlen($this->separator));

                $curtag = strrev($curtag);

                $this->current_tag = $curtag;

                $this->tagdata = '';

                return TRUE;

        }



        function characterData($parser, $cdata) {

                $this->tagdata .= $cdata;

        }





        // this is automatically called when the class is initialized

        function xml($data,$identifier='xml') {  



                $this->identifier = $identifier;



                // are we using php4 or better?

                //print phpversion();



                // create parser object

                $this->xml_parser = xml_parser_create();



                // set up some options and handlers

                xml_set_object($this->xml_parser,$this);

                xml_parser_set_option($this->xml_parser,XML_OPTION_CASE_FOLDING,0);

                xml_set_element_handler($this->xml_parser, "startElement", "endElement");

                xml_set_character_data_handler($this->xml_parser, "characterData");



                if (!xml_parse($this->xml_parser, $data, TRUE)) {

                        die(sprintf("XML error: %s at line %d",

                        xml_error_string(xml_get_error_code($this->xml_parser)),

                        xml_get_current_line_number($this->xml_parser)));

                }



                // we are done with the parser, so let's free it

                xml_parser_free($this->xml_parser);



        }





} // thus, we end our class xml



?>


Kod
<?

/*

* xml_parser.php

*

* @(#) $Header: /cvsroot/PHPlibrary/xml_parser.php,v 1.16 2001/09/19 02:51:46 mlemos Exp $

*

*/



/*

* Parser error numbers:

*

* 1 - Could not create the XML parser

* 2 - Could not parse data

* 3 - Could not read from input stream

*

*/



$xml_parser_handlers=array();



Function xml_parser_start_element_handler($parser,$name,$attrs)

{

  global $xml_parser_handlers;



    if(!strcmp($xml_parser_handlers[$parser]->error,""))

  $xml_parser_handlers[$parser]->StartElement($xml_parser_handlers[$parser],$name,$attrs);

}



Function xml_parser_end_element_handler($parser,$name)

{

  global $xml_parser_handlers;



    if(!strcmp($xml_parser_handlers[$parser]->error,""))

  $xml_parser_handlers[$parser]->EndElement($xml_parser_handlers[$parser],$name);

}



Function xml_parser_character_data_handler($parser,$data)

{

  global $xml_parser_handlers;



    if(!strcmp($xml_parser_handlers[$parser]->error,""))

  $xml_parser_handlers[$parser]->CharacterData($xml_parser_handlers[$parser],$data);

}



class xml_parser_handler_class

{

    var $xml_parser;

    var $error_number=0;

    var $error="";

    var $error_code=0;

    var $error_line,$error_column,$error_byte_index;

    var $structure=array();

    var $positions=array();

    var $path="";

    var $store_positions=0;

    var $simplified_xml=0;

    var $fail_on_non_simplified_xml=0;



    Function SetError(&$object,$error_number,$error)

    {

  $object->error_number=$error_number;

  $object->error=$error;

  $object->error_line=xml_get_current_line_number($object->xml_parser);

  $object->error_column=xml_get_current_column_number($object->xml_parser);

  $object->error_byte_index=xml_get_current_byte_index($object->xml_parser);

    }



    Function SetElementData(&$object,$path,&$data)

    {

  $object->structure[$path]=$data;

  if($object->store_positions)

  {

     $object->positions[$path]=array(

    "Line"=>xml_get_current_line_number($object->xml_parser),

    "Column"=>xml_get_current_column_number($object->xml_parser),

    "Byte"=>xml_get_current_byte_index($object->xml_parser)

     );

  }

    }



    Function StartElement(&$object,$name,&$attrs)

    {

  if(strcmp($this->path,""))

  {

     $element=$object->structure[$this->path]["Elements"];

     $object->structure[$this->path]["Elements"]++;

     $this->path.=",$element";

  }

  else

  {

     $element=0;

     $this->path="0";

  }

  $data=array(

     "Tag"=>$name,

     "Elements"=>0

  );

  if($object->simplified_xml)

  {

     if($object->fail_on_non_simplified_xml

     && count($attrs)>0)

     {

    $this->SetError($object,2,"Simplified XML can not have attributes in tags");

    return;

     }

  }

  else

     $data["Attributes"]=$attrs;

  $this->SetElementData($object,$this->path,$data);

    }



    Function EndElement(&$object,$name)

    {

  $this->path=(($position=strrpos($this->path,",")) ? substr($this->path,0,$position) : "");

    }



    Function CharacterData(&$object,$data)

    {

  $element=$object->structure[$this->path]["Elements"];

  $previous=$this->path.",".strval($element-1);

  if($element>0

  && GetType($object->structure[$previous])=="string")

     $object->structure[$previous].=$data;

  else

  {

     $this->SetElementData($object,$this->path.",$element",$data);

     $object->structure[$this->path]["Elements"]++;

  }

    }

};



class xml_parser_class

{

    var $xml_parser=0;

    var $parser_handler;

    var $error="";

    var $error_number=0;

    var $error_line=0;

    var $error_column=0;

    var $error_byte_index=0;

    var $error_code=0;

    var $stream_buffer_size=4096;

    var $structure=array();

    var $positions=array();

    var $store_positions=0;

    var $case_folding=0;

    var $target_encoding="ISO-8859-1";

    var $simplified_xml=0;

    var $fail_on_non_simplified_xml=0;



    Function xml_parser_start_element_handler($parser,$name,$attrs)

    {

  if(!strcmp($this->error,""))

     $this->parser_handler->StartElement($this,$name,$attrs);

    }



    Function xml_parser_end_element_handler($parser,$name)

    {

  if(!strcmp($this->error,""))

     $this->parser_handler->EndElement($this,$name);

    }



    Function xml_parser_character_data_handler($parser,$data)

    {

  if(!strcmp($this->error,""))

     $this->parser_handler->CharacterData($this,$data);

    }



    Function SetErrorPosition($error_number,$error,$line,$column,$byte_index)

    {

  $this->error_number=$error_number;

  $this->error=$error;

  $this->error_line=$line;

  $this->error_column=$column;

  $this->error_byte_index=$byte_index;

    }



    Function SetError($error_number,$error)

    {

  $this->error_number=$error_number;

  $this->error=$error;

  if($this->xml_parser)

  {

     $line=xml_get_current_line_number($this->xml_parser);

     $column=xml_get_current_column_number($this->xml_parser);

     $byte_index=xml_get_current_byte_index($this->xml_parser);

  }

  else

  {

     $line=$column=1;

     $byte_index=0;

  }

  $this->SetErrorPosition($error_number,$error,$line,$column,$byte_index);

    }



    Function Parse($data,$end_of_data)

    {

  global $xml_parser_handlers;



  if(strcmp($this->error,""))

     return($this->error);

  if(!$this->xml_parser)

  {

     if(!function_exists("xml_parser_create"))

     {

    $this->SetError(1,"XML support is not available in this php configuration");

    return($this->error);

     }

     if(!($this->xml_parser=xml_parser_create()))

     {

    $this->SetError(1,"Could not create the XML parser");

    return($this->error);

     }

     xml_parser_set_option($this->xml_parser,XML_OPTION_CASE_FOLDING,$this->case_folding);

     xml_parser_set_option($this->xml_parser,XML_OPTION_TARGET_ENCODING,$this->target_encoding);

     if(function_exists("xml_set_object"))

     {

    xml_set_object($this->xml_parser,$this);

    $this->parser_handler=new xml_parser_handler_class;

    $this->structure=array();

    $this->positions=array();

     }

     else

     {

    $xml_parser_handlers[$this->xml_parser]=new xml_parser_handler_class;

    $xml_parser_handlers[$this->xml_parser]->xml_parser=$this->xml_parser;

    $xml_parser_handlers[$this->xml_parser]->store_positions=$this->store_positions;

    $xml_parser_handlers[$this->xml_parser]->simplified_xml=$this->simplified_xml;

    $xml_parser_handlers[$this->xml_parser]->fail_on_non_simplified_xml=$this->fail_on_non_simplified_xml;

     }

     xml_set_element_handler($this->xml_parser,"xml_parser_start_element_handler","xml_parser_end_element_handler");

     xml_set_character_data_handler($this->xml_parser,"xml_parser_character_data_handler");

  }

  $parser_ok=xml_parse($this->xml_parser,$data,$end_of_data);

  if(!function_exists("xml_set_object"))

     $this->error=$xml_parser_handlers[$this->xml_parser]->error;

  if(!strcmp($this->error,""))

  {

     if($parser_ok)

     {

    if($end_of_data)

    {

        if(function_exists("xml_set_object"))

      Unset($this->parser_handler);

        else

        {

      $this->structure=$xml_parser_handlers[$this->xml_parser]->structure;

      $this->positions=$xml_parser_handlers[$this->xml_parser]->positions;

      Unset($xml_parser_handlers[$this->xml_parser]);

        }

        xml_parser_free($this->xml_parser);

        $this->xml_parser=0;

    }

     }

     else

    $this->SetError(2,"Could not parse data: ".xml_error_string($this->error_code=xml_get_error_code($this->xml_parser)));

  }

  else

  {

     if(!function_exists("xml_set_object"))

     {

    $this->error_number=$xml_parser_handlers[$this->xml_parser]->error_number;

    $this->error_code=$xml_parser_handlers[$this->xml_parser]->error_code;

    $this->error_line=$xml_parser_handlers[$this->xml_parser]->error_line;

    $this->error_column=$xml_parser_handlers[$this->xml_parser]->error_column;

    $this->error_byte_index=$xml_parser_handlers[$this->xml_parser]->error_byte_index;

     }     

  }

  return($this->error);

    }



    Function VerifyWhiteSpace($path)

    {

  if($this->store_positions)

  {

     $line=$parser->positions[$path]["Line"];

     $column=$parser->positions[$path]["Column"];

     $byte_index=$parser->positions[$path]["Byte"];

  }

  else

  {

     $line=$column=1;

     $byte_index=0;

  }

  if(!IsSet($this->structure[$path]))

  {

     $this->SetErrorPosition(2,"element path does not exist",$line,$column,$byte_index);

     return($this->error);

  }

  if(GetType($this->structure[$path])!="string")

  {

     $this->SetErrorPosition(2,"element is not data",$line,$column,$byte_index);

     return($this->error);

  }

  $data=$this->structure[$path];

  for($previous_return=0,$position=0;$position<strlen($data);$position++)

  {

     switch($data[$position])

     {

    case " ":

    case "t":

        $column++;

        $byte_index++;

        $previous_return=0;

        break;

    case "n":

        if(!$previous_return)

      $line++;

        $column=1;

        $byte_index++;

        $previous_return=0;

        break;

    case "r":

        $line++;

        $column=1;

        $byte_index++;

        $previous_return=1;

        break;

    default:

        $this->SetErrorPosition(2,"data is not white space",$line,$column,$byte_index);

        return($this->error);

     }

  }

  return("");

    }



    Function ParseStream($stream)

    {

  if(strcmp($this->error,""))

     return($this->error);

  do

  {

     if(!($data=fread($stream,$this->stream_buffer_size)))

     {

    if(!feof($stream))

    {

        $this->SetError(3,"Could not read from input stream");

        break;

    }

     }

     if(strcmp($error=$this->Parse($data,feof($stream)),""))

    break;

  }

  while(!feof($stream));

  return($this->error);

    }



    Function ParseFile($file)

    {

  if(!file_exists($file))

     return("the XML file to parse ($file) does not exist");

  if(!($definition=fopen($file,"r")))

     return("could not open the XML file ($file)");

  $error=$this->ParseStream($definition);

  fclose($definition);

  return($error);

    }

};



Function XMLParseFile(&$parser,$file,$store_positions,$cache="",$case_folding=0,$target_encoding="ISO-8859-1",$simplified_xml=0,$fail_on_non_simplified_xml=0)

{

    if(!file_exists($file))

  return("the XML file to parse ($file) does not exist");

    if(strcmp($cache,""))

    {

  if(file_exists($cache)

  && filectime($file)<=filectime($cache))

  {

     if(($cache_file=fopen($cache,"r")))

     {

    if(function_exists("set_file_buffer"))

        set_file_buffer($cache_file,0);

    if(!($cache_contents=fread($cache_file,filesize($cache))))

        $error="could not read from the XML cache file $cache";

    else

        $error="";

    fclose($cache_file);

    if(!strcmp($error,""))

    {

        if(GetType($parser=unserialize($cache_contents))=="object"

        && IsSet($parser->structure))

        {

      if(!IsSet($parser->simplified_xml))

          $parser->simplified_xml=0;

      if(($simplified_xml

      || !$parser->simplified_xml)

      && (!$store_positions

      || $parser->store_positions))

      {

          return("");

      }

        }

        else

      $error="it was not specified a valid cache object in XML file ($cache)";

    }

     }

     else

    $error="could not open cache XML file ($cache)";

     if(strcmp($error,""))

    return($error);

  }

    }

    $parser=new xml_parser_class;

    $parser->store_positions=$store_positions;

    $parser->case_folding=$case_folding;

    $parser->target_encoding=$target_encoding;

    $parser->simplified_xml=$simplified_xml;

    $parser->fail_on_non_simplified_xml=$fail_on_non_simplified_xml;

    if(!strcmp($error=$parser->ParseFile($file),"")

    && strcmp($cache,""))

    {

  if(($cache_file=fopen($cache,"w")))

  {

     if(function_exists("set_file_buffer"))

    set_file_buffer($cache_file,0);

     if(!fwrite($cache_file,serialize($parser)))

    $error="could to write to the XML cache file ($cache)";

     fclose($cache_file);

     if(strcmp($error,""))

    unlink($cache);

  }

  else

     $error="could not open for writing to the cache file ($cache)";

    }

    return($error);

}



?>


sorry za wklepywanie tego do posta ale nie pamietam skad moza sciagnac te classy.
castor
tu napewno cos dla siebie znajdziesz:
http://www.phpclasses.org/search.html?word...xml&go_search=1 :wink:
GeoS
castor: dzieki smile.gif Kompletnie zapomnielem o tej witrynie :oops:

Jutro wszystko potestuje.

Wymagania sa takie, ze potrzebuje rozwiazania radzacego sobie z > 5000 odwiedzajacych na dobe (baza MySQL i czasem Apache sie zatykaja, wiec trzeba przejsc na jakies szybsze rozwiazanie).
Mam pliki XMLowe i chce tylko wyciagnac z nich dane np. do tablicy php. Nie chce sie bawic z XSLT i cala reszta tego balaganu, bo musze miec dostep do PHPa.
Z tego co sie orientuje w kwestii wydajnosci, to serwerek WWW stoi na jakims PIII.
Pozyjemy, zobaczymy smile.gif
GeoS
Pierwsza klasa Seth'a zdaje sie byc wystarczajaca i patrzac na objetosc kodu wystarczajaco prosta (w porownaniu do pozostalych rozwiazan) smile.gif
To jest wersja lo-fi głównej zawartości. Aby zobaczyć pełną wersję z większą zawartością, obrazkami i formatowaniem proszę kliknij tutaj.
Invision Power Board © 2001-2025 Invision Power Services, Inc.