Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> PHP & XML
slash.
post
Post #1





Grupa: Zarejestrowani
Postów: 113
Pomógł: 0
Dołączył: 7.08.2003
Skąd: Warszawa | Jankowce

Ostrzeżenie: (0%)
-----


Mam plik XML ktory zawiera drzewo w stylu:
[xml:1:36e3dfce81]
<core>
<element>
<typ>Jakis tam typ</typ>
<typid>1</typid>
</element>
<element>
<typ>Drugi element</typ>
<typid>2</typid>
</element>
<element>
<typ>trzeci element</typ>
<typid>3</typid>
</element>

<grupaelementow>
<nazwa>grupa1</nazwa>
<subnazwa>aaaa</subnazwa>
<typid>1</typid>
<typid>2</typid>
</grupaelementow>
<grupaelementow>
<nazwa>grupa2</nazwa>
<subnazwa>qqqq</subnazwa>
<typid>1</typid>
</grupaelementow>
<grupaelementow>
<nazwa>grupa3</nazwa>
<subnazwa>ttt</subnazwa>
<typid>1</typid>
<typid>2</typid>
<typid>2</typid>
</grupaelementow>
</core>
[/xml:1:36e3dfce81]
I teraz chcialbym stworzyc tablice wielowymiarowa na podstawie tego pliku xml, ktora by wygladala tak:
[php:1:36e3dfce81]<?php

$core['nazwa grupy']['subnazwa']['typid'] = 'nazwa typu';
//czyli tak zeby miec to wszystko pogrupowane czyli przykladowo core od
//grupa elementow o nazwie grupa1 wygladalby tak:
$core['grupa1']['aaaa']['1'] = 'Jakis tam typ';
$core['grupa1']['aaaa']['2'] = 'Drugi element';

?>[/php:1:36e3dfce81]
w sposob analogiczny pozostale typy - czyli chodzi mi o przerobienie dokumentu xml do postaci tablicy wielowymiarowej, i teraz pytanie jak to zrobic przy pomocy php - potraktowac dokument xml jako dokument tekstowy i uzywac fuknkcji do obrobki tekstu wraz z wyrazeniami regularnym, czy jest moze jakis sposob poprzez wbudowane funkcje obslugi XML - a moze jakies 3 rozwiazanie (IMG:http://forum.php.pl/style_emoticons/default/questionmark.gif)
Prosze o pomoc
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi
Dawid Pytel
post
Post #2





Grupa: Zarejestrowani
Postów: 133
Pomógł: 0
Dołączył: 15.09.2003

Ostrzeżenie: (0%)
-----


Wprawdzie poniższa funkcja nie stworzy tablic w takiej postaci jakiej chciałeś (jednak równie 'przyjaźnie') ale też poradzi sobie bezproblemowo z Twoim plikiem XML:

  1. <?php
  2.  
  3. function fetch_xml($xml){ 
  4.  
  5. if(!($fp = fopen($xml, &#092;"r\"))){ 
  6. die(&#092;"IO Error $xml_file\"); 
  7. } 
  8. while($chunk = fread($fp,4096)){ 
  9. $xml_data .= $chunk; 
  10. } 
  11.  
  12. $parser = xml_parser_create(); 
  13. xml_parse_into_struct($parser, $xml_data, &$assoc_arr, &$idx_arr); 
  14. xml_parser_set_option($parser, XML_OPTION_CASE_FOLDING, 0); 
  15. xml_parser_set_option($parser, XML_OPTION_SKIP_WHITE, 1); 
  16.  
  17. $root_tag = $assoc_arr[$root_level]['tag'];
  18. $base_level = $root_level+1; 
  19. $base_tag = strtolower($assoc_arr[$base_level]['tag']); 
  20. $i = 0; 
  21. $prefix = &#092;"\"; 
  22.  
  23. foreach($assoc_arr as $element)
  24. { 
  25. if( $element['tag'] != $root_tag )
  26. { 
  27. if(!preg_match('/^s+$/', $element['value']))
  28. { 
  29. $tag = strtolower($element['tag']); 
  30. if($element['type'] == 'open' && $element['level'] > ($base_level+1))
  31. { 
  32. // Dodanie noweg prefixa 
  33. $tag_prefix[$element['level']] = $tag; 
  34. } elseif($element['type'] == 'close' && $element['level'] > ($base_level+1)) { 
  35.  
  36. // Usuwa prefix gdy koniec tagu 
  37. $tag_prefix[$element['level']] = &#092;"\"; 
  38.  
  39. } elseif($element['type'] == 'complete') { 
  40.  
  41. // Generuje prefix
  42. if(count($tag_prefix) > 0)
  43. { 
  44. $str_prefix = &#092;"\"; 
  45.  
  46. foreach($tag_prefix as $prefix)
  47. { 
  48. if(!empty($prefix)) 
  49. $str_prefix .= $prefix.&#092;"_\"; 
  50. } 
  51.  
  52. } // Koniec prefixa 
  53.  
  54. // Dodaje dane do tablicy 
  55. $extra = &#092;"\"; 
  56.  
  57. while(isset($items[$i][$str_prefix.$tag.$extra]))
  58. { 
  59. if(empty($extra)) 
  60. $extra = 1; 
  61. $extra++; 
  62. } 
  63. $items[$i][$str_prefix.$tag.$extra] = $element['value']; 
  64. } 
  65. } 
  66. if($tag == $base_tag)
  67. { 
  68. // Dodaj atrybut 
  69. if(isset($element['attributes']) && $element['type'] == 'open'){ 
  70.  $items[$i] = $element['attributes']; 
  71. } 
  72. // Nowy rekord 
  73. if($element['type'] == 'close'){ 
  74. $i++; 
  75. } 
  76. } 
  77. } 
  78. } 
  79.  
  80. return $items; 
  81. }
  82.  
  83. ?>



w paramterze podajesz nazwę pliku XML.
To powinno wystarczyć.

Ten post edytował Dawid Pytel 7.04.2006, 16:14:07
Go to the top of the page
+Quote Post

Posty w temacie


Reply to this topicStart new topic
2 Użytkowników czyta ten temat (2 Gości i 0 Anonimowych użytkowników)
0 Zarejestrowanych:

 



RSS Aktualny czas: 3.10.2025 - 04:22