Uniwersal Import Xml
- Szczegóły
- Kategoria: Class
- Opublikowano: niedziela, 03, sierpień 2014 18:41
- Super User
- Odsłony: 57965
Uniwersal Import Xml
Same działanie klasy jest banalnie proste. Podajemy ścieżkę z tablicy $_FILES i $_POST. $_FILES
zawiera nasz>plik .xml do importu, a $_POST znaczniki, które chcemy zaimportować.
Dla lepszego zobrazowania działania pokażę formularz i znajdujące się tam dane.
Potrzebne dane do importu bierzemy z samego pliku .xml, które nas interesują. Pierwsze pole formularz przypisujemy nasz zbiór i
podzbiór oddzielone && , a potem w zależności od zapotrzebowania np. chcemy uzyskać takie dane jak : id, cena, tytuł, url,
trzy z nich są selektorami, a jeden to Tag pliku .xml. Teraz tak tytuł jest w tagach „<tytuł>Nasz tytuł</ tytuł >” , a
reszta danych znajduje się w tagch jak i w selektorach
<dane id=”123” cena=”34,66” url=”http://www.example.com”></dane>.
W polach formularza przypiszemy takie wartości.
1. nasz zbiór i podzbiór „zbior&&podzbior”
2. nasz tytuł w tagu „tytuł”
3. nasze dane w Tagu i selektorze „dane&&id”
4. nasze dane w Tagu i selektorze „dane&&cena”
5. nasze dane w Tagu i selektorze „dane&&url”

Reszta danych w pliku nas nie interesuje tak jakby jej tam nie było.
I tak wysłany formularz zwróci nam potrzebne dane w postaci tablicy.
Następny przykład jeżeli plik .xml ma w sobie dane w postaci tylko tagi <?> to wpisujemy
- nasz zbiór i podzbiór „zbior&&podzbior”
- nasz tytuł w tagu „tytuł”
- nasza cena w Tagu „cena”
- nasz url w Tagu „url”

I tak wysłany formularz zwróci nam potrzebne dane w postaci tablicy.
Prawda że to rozwiązanie jest bardziej przyjazne programiście niż dostosowywanie za każdym razem importera pod dany plik. Dla mnie jest to bardzo przydatne.
Może i wam ułatwi pracę w jakiś tam sposób.
test.php
test-post.php
include("uniwersal_import_xml.class.php"); $import_xml = new uniwersal_import_xml($_FILES); $import_xml->save_xml(); $import_xml->wczytaj_post($_POST['MARKA'], $_POST['MODEL'], $_POST['CENA'], $_POST['ID'], $_POST['VAT'], $_POST['URL_IMG'], $_POST['URL_PROD'], $_POST['OPIS']); ### nasze aukcje ### $ff = $import_xml->uniwersal_plik_xml(); //// print_r($ff); for($i=0; $i < count($ff['MARKA']); $i++){ // print_r($ff); ### wyświetlamy ### if(!empty($ff['MARKA'])){ echo "".$i."ID ".$ff['ID'][$i].""; echo "".$i."marka ".$ff['MARKA'][$i].""; echo ""."model ".$ff['MODEL'][$i].""; echo "".$i."cena ".$ff['CENA'][$i].""; // echo $ff['CENA_NETTO'][$i].""; echo "".$i.$ff['VAT'][$i].""; echo "".$i.$ff['URL_IMG'][$i].""; echo "".$i.$ff['URL_PROD'][$i].""; echo "".$i."opis ".$ff['OPIS'][$i].""; } }
uniwersal_import_xml.class.php
#The MIT License ############################### #Copyright (c) 2014 Adam Berger ############################### #Permission is hereby granted, free of charge, to any person obtaining a copy #of this software and associated documentation files (the "Software"), to deal #in the Software without restriction, including without limitation the rights #to use, copy, modify, merge, publish, distribute, sublicense, and/or sell #copies of the Software, and to permit persons to whom the Software is #furnished to do so, subject to the following conditions: ############################### #The above copyright notice and this permission notice shall be included in #all copies or substantial portions of the Software. ############################### #THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR #IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, #FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE #AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER #LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, #OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN #THE SOFTWARE. ############################## /** * PHP 5.1 */ /** * @version 2.00 * Full uniwersal import xml * @author Adam Berger <ber34#o2.pl--> * Class allows you to import a variety of files. * Xml assigning appropriate tags that you want to read. * We read the same data from different files. * The structure of the document must remain the same in each of them. * Tags that you want to display the message given in the table. * * Klasa pozwala na import różnych plików .xml z przypisaniem odpowiednich tagów, * które chcemy odczytać. * Możemy odczytać te same dane z różnych plików. * Struktura dokumentu musi pozostać taka sama w każdym z nich. * Tagi które chcemy wyświetlić podajemy w tablicy post. * Pełny uniwersalny kontapitybilny z ceneo, nokut, okazje, aukcyjny i jeszcze parę innych */ class uniwersal_import_xml { public $file = array(); public $type; public $xml_name; public $types; public $ile_xml; public $kk; public $ww; public $pp = array(); public $bb = array(); public $error = false; public $post = array(); public $t = array(); public function __construct($files) { if(!empty($files)){ $this->file[] = $files; list($kkk, $this->ww)=each($this->file[0]); } if(array_key_exists($kkk, $this->file[0])){ $this->kk = $kkk; }else{ $this->kk = $this->error; } $this->error; } ### nazwa pliku. ### public function files_xml(){ if(!empty($this->file)){ return $this->kk; }else{ return $this->error; } } ### nazwa pliku. ### public function files_name_xml(){ if(!empty($this->file)){ return $this->ww['name']; }else{ return $this->error; } } ### temp nazwa pliku. ### public function files_temp_name_xml(){ if(!empty($this->file)){ return $this->ww['tmp_name']; }else{ return $this->error; } } ### Typ pliku. ### public function files_types_xml(){ if(!empty($this->file)){ return $this->ww['type']; }else{ return $this->error; } } ### Błąd pliku. ### public function files_error_xml(){ if(!empty($this->file)){ return $this->ww['error']; }else{ return $this->error; } } ### Rozmiar pliku. ### public function files_size_xml(){ if(!empty($this->file)){ return $this->ww['size']; }else{ return $this->error; } } ### Typ pliku. ### public function is_typee(){ $typee = array('text/xml'); if(in_array(self::files_types_xml(), $typee)){ return true; }else{ return $this->error; } } ### wybierz który import swój czy obcy. ### public function select_tag_xml(){ if($this->kk == "fileimportxml"){ return true; }else{ return $this->error; } } ## zamienienie i rozdzielenie ## protected function load_xml(){ if(file_exists("xml/".basename($this->files_name_xml()))){ // $this->xml_name if(self::files_name_xml()){ $xml= new DOMDocument('1.0', 'utf-8');// iso-8859-2 $xml->preserveWhiteSpace=false; $xml->loadXML(file_get_contents("xml/".basename($this->files_name_xml()))); // $this->xml_name if($xml){ return $xml; }else{ return $this->error; } }else{ return $this->error; } } } #### pobieramy tablice post #### public function wczytaj_post(){ if(func_num_args()>0){ $this->post_a = func_get_args(); foreach($_POST as $bb =>$pp){ if(in_array($pp,$this->post_a)){ $this->pp[] = strip_tags($pp); if(array_key_exists($bb, $_POST)){ $this->bb[] = array($bb => strip_tags($pp)); } } } } } ### zapisz plik ### public function save_xml(){ $error =""; if(!file_exists($this->files_name_xml())){ if ($this->files_error_xml() == '0') { $scie = "xml/".basename($this->files_name_xml()); //// gdzie wgrać plik //$scie = strtolower(strtr($scie, "ąęćłńóśźżĄĆĘŁŃÓŚŹŻ", "aeclnoszzACELNOSZZ")); /// wyczyszczenie nazwy + ustawienie małe litery if(is_uploaded_file($this->files_temp_name_xml())){ if($this->files_size_xml() == '0' or $this->files_size_xml() > '4096000'){ /////Sprawdź czy jest mniejszy od 4MB @$this->error .= "Stop Plik większy Od 4MB"; }elseif($this->is_typee() !== true){ /// sprawdz czy zdięcie - image @$this->error .= "Stop Plik nie jest xml"; }else{ move_uploaded_file($this->files_temp_name_xml(), $scie); /// gdzie zapisac @$this->error.= "Zapis Udany"; } }else{ @$this->error.= "Stop Błąd Pliku"; } } } return $this->error; } public function uniwersal_plik_xml(){ $t=array(); if($xml = $this->load_xml()){ $domXPath = new DOMXPath($xml); foreach ($domXPath->query('//*') as $keyDOM) { foreach($this->bb as $sss){ foreach($sss as $kkkkkkk => $ssssssss){ $s = explode('&&', $ssssssss); if(!empty($s[1])){ if($keyDOM->getAttribute($s[1]) && $keyDOM->tagName == $s[0]){ $MARKA = $keyDOM->getAttribute($s[1]); @$t[$kkkkkkk][] = $MARKA; // for } }else if($keyDOM->tagName == $s[0]){ if(in_array($s[0],$this->pp)){ $t[$kkkkkkk][] = $keyDOM->nodeValue; } } } } } } return $t; } }
Klasa Uniwerasl Import Xml współpracuje z „ceneo”, „nokaut”, „okazje” „aukcyjny” i jeszcze parę innych.
Jeżeli masz plik i struktura dokumentu jest taka jak w plikach wyżej wymienionych,
to twój plik też powinien współpracować z klasą Uniwerasl Import Xml.
Jeżeli nie jesteś pewien,to z kontaktuj się ze mną, a ja postaram się wyjaśnić wszystko w czym masz problem.