Klasa Uniwersal Import Xml
|Klasa Uniwersal Import Xml
Podczas wykonywania różnych zleceń z plikami .xml napotykałem na problem , który mnie zawsze irytował. Problem polegał na tym, że za każdym razem musiałem robić nowy integrator pod dany plik .xml i danych się tam znajdujących.
Jeżeli kiedykolwiek używaliście takie pliki i importowaliście dane do serwisu to zapewne spotkaliście się z takim problemem. Niektóre pliki .xml nie pasowały i nie szło przeprowadzić importu zawartości owego pliku.
Pomysł
Postanowiłem zrobić klasę uniwersalną,
żeby każdy , no prawie każdy plik pasował i szło go zaimportować i odczytać. Pomysł oparłem na klasie którą nazwałem
„Uniwersal Import Xml „ teraz w łatwy sposób mogę przeprowadzić operację na plikach .xml. Efekt mojej pracy chciałem wam
przedstawić.
Działanie
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.
Wykonanie
Teraz taki tytuł wygląda tak:
<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”
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 | <?xml version="1.0" encoding="ISO-8859-2"?><PRODUKTY> <PRODUKT><ID><![CDATA[70]]></ID> <KATEGORIA><![CDATA[Smartfony]]></KATEGORIA> <MARKA><![CDATA[300]]></MARKA> <MODEL><![CDATA[TELEFON ]]></MODEL> <CENA><![CDATA[198.71]]></CENA> <URL_IMG><![CDATA[http://www.exsample.com/data/jhh.jpg]]></URL_IMG> <URL_PROD><![CDATA[http://www.exsample.com/gg.html]]></URL_PROD> <OPIS><![CDATA[TELEFON I930 ]]></OPIS> </PRODUKT> <PRODUKT> <ID><![CDATA[79]]></ID> <KATEGORIA><![CDATA[kategoria]]></KATEGORIA> <MARKA><![CDATA[HTC]]></MARKA> <MODEL><![CDATA[NOWY HTC GW]]></MODEL> <CENA><![CDATA[347]]></CENA> <URL_IMG><![CDATA[http://www.exsample.com/data/gg.jpg]]></URL_IMG> <URL_PROD><![CDATA[http://www.exsample.com/gg.html]]></URL_PROD> <OPIS><![CDATA[dfsdfsdfsdfsdfsdfsdfsdfsdfsd d sd d sssfsdfsdfsf]]></OPIS> </PRODUKT> <PRODUKT> <ID><![CDATA[72]]></ID> <KATEGORIA><![CDATA[kategoria]]></KATEGORIA> <MARKA><![CDATA[LG]]></MARKA> <MODEL><![CDATA[LG]]></MODEL> <CENA><![CDATA[298.71]]></CENA> <URL_IMG><![CDATA[http://www.exsample.com/data/hh.jpg]]></URL_IMG> <URL_PROD><![CDATA[http://www.exsample.com.html]]></URL_PROD> <OPIS><![CDATA[ Description of document ]]></OPIS> </PRODUKT> </PRODUKTY> |
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.
Uniwersal Import Xml
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 | <form id="" action="test-post.php" method="post" enctype="multipart/form-data"> <label>Wczytaj Plik Obcy:</label> <input type="text" name="ZBIOR"><strong style="color: red;">* Zbiór znaczników podzielony &&</strong> <input type="text" name="MARKA"><strong style="color: red;">* Tytuł</strong> <input type="text" name="MODEL"><strong style="color: red;">*</strong> <input type="text" name="CENA"><strong style="color: red;">*</strong> <input type="text" name="CENA_NETTO"> <input type="text" name="VAT"> <input type="text" name="ID"> <input type="text" name="URL_IMG"><strong style="color: red;">*</strong> <input type="text" name="URL_PROD"> <input type="text" name="OPIS"><strong style="color: red;">*</strong> <input type="file" name="fileimportxml" accept="text/xml"> <input id="" class="przycisk" type="submit" value="Zapisz >>"> </form> |
test-post.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 | /* require("class-import-xml.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(); for($i=0; $i < count($ff['MARKA']); $i++) { ### 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].""; } } |
class-import-xml.php
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 | #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 * @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óww .xml z przypisaniem odpowiednich tagach, * 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, aukcyjny, */ class uniwersal_import_xml { public $file = array(); public $type; public $xml_name; public $types; public $ile_xml; public $kk; public $ww; public $pp; public $bb; 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)); } } } } } ### zilicz ile ### public function count_xml(){ //print_r($this->bb); //// $this->ile_xml = count($this->bb); return $this->ile_xml; } ### zapisz plik ### public function save_xml(){ $error =""; if(!file_exists($this->files_name_xml())){ if ($this->files_error_xml() == '0') { // gdzie wgrac plik $scie = "xml/".basename($this->files_name_xml()); if(is_uploaded_file($this->files_temp_name_xml())){ if($this->files_size_xml() == '0' or $this->files_size_xml() > '4096000') { //Sprawdz czy jest mniejszy od 4MB @$this->error .= "<b style='color: #ff9601;'>Stop Plik większy Od 4MB</b>"; }elseif($this->is_typee() !== true) { /// sprawdz czy - image @$this->error .= "<b style='color: #ff9601;'>Stop Plik nie jest xml</b>"; }else{ move_uploaded_file($this->files_temp_name_xml(), $scie); /// gdzie zapisac @$this->error.= "<b style='color: #ff9601;'>Zapis Udany</b>"; } }else{ @$this->error.= "<b style='color: #ff9601;'>Stop Błąd Pliku</b>"; } } } 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])){ $this->ile_xml = count($s[0]); if($keyDOM->getAttribute($s[1]) && $keyDOM->tagName == $s[0]){ $MARKA = $keyDOM->getAttribute($s[1]); @$t[$kkkkkkk][] = $MARKA; // for // @$t[$kkkkkkk] = $MARKA; // foreach } }else if($keyDOM->tagName == $s[0]){ if(in_array($s[0],$this->pp)){ @$t[$kkkkkkk][] = $keyDOM->nodeValue; //@$t[$kkkkkkk] = $keyDOM->nodeValue; } } } } } } return $t; } } |
Pobieranie
Download całości z plikami .xml do testów tutaj: Uniwersal Import XML
🙂