Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Zapis pliku XML do Bazy MYSQL
Forum PHP.pl > Forum > PHP
wojtek992
Witam

Wiem, że już raz czy dwa było o tym na forum, ale jakoś nie mogę dopasować się do tych rozwiązań.
Za pomocą klasy DOMDocument mam napisać w php program służący do odczytania dokumentu i zapisania go w bazie danych (MYSQL).

Wiem, że trzeba skorzystać z XPATH, ale za bradzo nie wiem z której strony to ugryźć, wychodzą mi bardzo długie kody z tego. Jakieś małe wskazówki ?

Plik XML

  1.  
  2. <?xml version="1.0" standalone="no"?>
  3. <?xml-stylesheet type="text/xsl" href="WDZ.xsl"?>
  4. <!DOCTYPE WEZWANIEDOZAPLATY SYSTEM "WDZ.dtd">
  5. <WEZWANIEDOZAPLATY>
  6. <WIERZYCIEL>
  7. <NAZWAW>Firma Krzak Sp.z.o.o.</NAZWAW>
  8. <ADRESW>Osiedlowa</ADRESW>
  9. <NUMERBUDYNKUW>3A</NUMERBUDYNKUW>
  10. <NUMERLOKALUW>8</NUMERLOKALUW>
  11. <KODPOCZTOWYW>83-400</KODPOCZTOWYW>
  12. <MIASTOW>Warszawa</MIASTOW>
  13. <NIPW>591-117-42-35</NIPW>
  14. </WIERZYCIEL>
  15. <MIEJSCEDATA>
  16. <MIEJSCE>Warszawa</MIEJSCE>
  17. <DATA>13.10.2011</DATA>
  18. </MIEJSCEDATA>
  19. <DLUZNIK>
  20. <NAZWAD>Jan Kowalski</NAZWAD>
  21.  
  22. <ADRESD>Robotnicza</ADRESD>
  23. <NUMERBUDYNKUD>8</NUMERBUDYNKUD>
  24. <NUMERLOKALUD>1</NUMERLOKALUD>
  25.  
  26. <KODPOCZTOWYD>22-034</KODPOCZTOWYD>
  27. <MIASTOD>Sopot</MIASTOD>
  28. <NIPD>591-117-42-35</NIPD>
  29. </DLUZNIK>
  30. <ZESTAWIENIE>
  31. <FAKTURA>
  32. <LP>1</LP>
  33. <NRFAKTURY>102/2010</NRFAKTURY>
  34. <DATA>11.10.2011</DATA>
  35. <KWOTANETTO>200</KWOTANETTO>
  36. <KWOTABRUTTO>246</KWOTABRUTTO>
  37. <TERMINPLATNOSCI>25.10.2011</TERMINPLATNOSCI>
  38. <POZOSTAJEDOZAPLATY>246</POZOSTAJEDOZAPLATY>
  39. </FAKTURA>
  40. <FAKTURA>
  41. <LP>2</LP>
  42. <NRFAKTURY>1025/2010</NRFAKTURY>
  43. <DATA>11.10.2011</DATA>
  44. <KWOTANETTO>2200</KWOTANETTO>
  45. <KWOTABRUTTO>2433</KWOTABRUTTO>
  46. <TERMINPLATNOSCI>26.10.2011</TERMINPLATNOSCI>
  47. <POZOSTAJEDOZAPLATY>5463</POZOSTAJEDOZAPLATY>
  48. </FAKTURA>
  49. <SUMA>1230,05</SUMA>
  50. </ZESTAWIENIE>
  51. <SUMASLOWNIE>jeden tysiąc dwieście trzydzieści złotych i pięć groszy</SUMASLOWNIE>
  52. <DANEBANKU>
  53. <NAZWA>Mbank</NAZWA>
  54. <NUMERKONTA>50 1020 5558 1111 1594 6590 0010</NUMERKONTA>
  55. </DANEBANKU>
  56. </WEZWANIEDOZAPLATY>
  57.  
  58.  


Tabele w bazie danych miejwięcej tego typu :
Wierzyciel
IDWierzyciel integer;
Nazwa char;
Adres char;

itd itd

Za wszelką okazaną pomoc będę bardzo wdzięczny

Pozdrawiam Wojtek
matrik
jest taka fajna funkcja do parsowania plików XML do tablicy smile.gif
Nie pamiętam skąd tę funkcje wytrzasnąłem już haha.gif
  1. function xml2array($contents, $get_attributes=1, $priority = 'tag'){
  2. if(!$contents) return array();
  3.  
  4. if(!function_exists('xml_parser_create')){
  5. return array();
  6. }
  7.  
  8. //Get the XML parser of PHP - PHP must have this module for the parser to work
  9. $parser = xml_parser_create('');
  10. xml_parser_set_option($parser, XML_OPTION_TARGET_ENCODING, "UTF-8"); # <a href="http://minutillo.com/steve/weblog/2004/6/17/php-xml-and-character-encodings-a-tale-of-sadness-rage-and-data-loss" target="_blank">http://minutillo.com/steve/weblog/2004/6/1...e-and-data-loss</a>
  11. xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0);
  12. xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1);
  13. xml_parse_into_struct($parser, trim($contents), $xml_values);
  14. xml_parser_free($parser);
  15.  
  16. if(!$xml_values) return;//Hmm...
  17.  
  18. //Initializations
  19. $xml_array = array();
  20. $parents = array();
  21. $opened_tags = array();
  22. $arr = array();
  23.  
  24. $current = &$xml_array; //Refference
  25.  
  26. //Go through the tags.
  27. $repeated_tag_index = array();//Multiple tags with same name will be turned into an array
  28. foreach($xml_values as $data){
  29. unset($attributes,$value);//Remove existing values, or there will be trouble
  30.  
  31. //This command will extract these variables into the foreach scope
  32. // tag(string), type(string), level(int), attributes(array).
  33. extract($data);//We could use the array by itself, but this cooler.
  34.  
  35. $result = array();
  36. $attributes_data = array();
  37.  
  38. if(isset($value)){
  39. if($priority == 'tag') $result = $value;
  40. else $result['value'] = $value; //Put the value in a assoc array if we are in the 'Attribute' mode
  41. }
  42.  
  43. //Set the attributes too.
  44. if(isset($attributes) and $get_attributes){
  45. foreach($attributes as $attr => $val){
  46. if($priority == 'tag') $attributes_data[$attr] = $val;
  47. else $result['attr'][$attr] = $val; //Set all the attributes in a array called 'attr'
  48. }
  49. }
  50.  
  51. //See tag status and do the needed.
  52. if($type == "open") {//The starting of the tag '<tag>'
  53. $parent[$level-1] = &$current;
  54. if(!is_array($current) or (!in_array($tag, array_keys($current)))){//Insert New tag
  55. $current[$tag] = $result;
  56. if($attributes_data) $current[$tag. '_attr'] = $attributes_data;
  57. $repeated_tag_index[$tag.'_'.$level] = 1;
  58.  
  59. $current = &$current[$tag];
  60.  
  61. } else { //There was another element with the same tag name
  62.  
  63. if(isset($current[$tag][0])) {//If there is a 0th element it is already an array
  64. $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
  65. $repeated_tag_index[$tag.'_'.$level]++;
  66. } else {//This section will make the value an array if multiple tags with the same name appear together
  67. $current[$tag] = array($current[$tag],$result);//This will combine the existing item and the new item together to make an array
  68. $repeated_tag_index[$tag.'_'.$level] = 2;
  69.  
  70. if(isset($current[$tag.'_attr'])){//The attribute of the last(0th) tag must be moved as well
  71. $current[$tag]['0_attr'] = $current[$tag.'_attr'];
  72. unset($current[$tag.'_attr']);
  73. }
  74.  
  75. }
  76. $last_item_index = $repeated_tag_index[$tag.'_'.$level]-1;
  77. $current = &$current[$tag][$last_item_index];
  78. }
  79.  
  80. } elseif($type == "complete"){//Tags that ends in 1 line '<tag />'
  81. //See if the key is already taken.
  82. if(!isset($current[$tag])){//New Key
  83. $current[$tag] = $result;
  84. $repeated_tag_index[$tag.'_'.$level] = 1;
  85. if($priority == 'tag' and $attributes_data) $current[$tag. '_attr'] = $attributes_data;
  86.  
  87. } else { //If taken, put all things inside a list(array)
  88. if(isset($current[$tag][0]) and is_array($current[$tag])) {//If it is already an array...
  89.  
  90. // ...push the new element into that array.
  91. $current[$tag][$repeated_tag_index[$tag.'_'.$level]] = $result;
  92.  
  93. if($priority == 'tag' and $get_attributes and $attributes_data){
  94. $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
  95. }
  96. $repeated_tag_index[$tag.'_'.$level]++;
  97.  
  98. } else { //If it is not an array...
  99. $current[$tag] = array($current[$tag],$result); //...Make it an array using using the existing value and the new value
  100. $repeated_tag_index[$tag.'_'.$level] = 1;
  101. if($priority == 'tag' and $get_attributes){
  102. if(isset($current[$tag.'_attr'])){//The attribute of the last(0th) tag must be moved as well
  103.  
  104. $current[$tag]['0_attr'] = $current[$tag.'_attr'];
  105. unset($current[$tag.'_attr']);
  106. }
  107.  
  108. if($attributes_data){
  109. $current[$tag][$repeated_tag_index[$tag.'_'.$level] . '_attr'] = $attributes_data;
  110. }
  111. }
  112. $repeated_tag_index[$tag.'_'.$level]++; //0 and 1 index is already taken
  113. }
  114. }
  115.  
  116. } elseif($type == 'close'){//End of tag '</tag>'
  117. $current = &$parent[$level-1];
  118. }
  119. }
  120.  
  121. return($xml_array);
  122. }


Mam nadzieję, że się przyda
wojtek992
NO tak wszystko by działało, tylko muszę to koniecznie zrobić w oparciu o klasę DOMdocument , ale dzięki za pomoc
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.