Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Generowanie tablicy z pliku wsadowego XML
Forum PHP.pl > Forum > PHP
troian
Witam, napisałem sobie funkcję która na podstawie tablicy:
  1. $table = array(
  2. array('id' => 1, 'parent_id' => 0, 'name' => 'page', 'value' => 'index'),
  3. array('id' => 2, 'parent_id' => 0, 'name' => 'mainMenu', 'value' => ''),
  4. array('id' => 3, 'parent_id' => 2, 'name' => 'option', 'value' => ''),
  5. array('id' => 4, 'parent_id' => 3, 'name' => 'url', 'value' => 'home'),
  6. array('id' => 5, 'parent_id' => 3, 'name' => 'name', 'value' => 'Strona Główna'),
  7. array('id' => 6, 'parent_id' => 2, 'name' => 'option', 'value' => ''),
  8. array('id' => 4, 'parent_id' => 6, 'name' => 'url', 'value' => 'register'),
  9. array('id' => 5, 'parent_id' => 6, 'name' => 'name', 'value' => 'rejestracja'),
  10. );


Generuje mi plik xml:
  1. <?xml version="1.0" encoding="utf-8"?>
  2. <ApplicationInfo>
  3. <page>index</page>
  4. <mainMenu>
  5. <option>
  6. <url>home</url>
  7. <name>Strona Główna</name>
  8. </option>
  9. <option>
  10. <url>register</url>
  11. <name>rejestracja</name>
  12. </option>
  13. </mainMenu>
  14. </ApplicationInfo>


Teraz mam pytanie w jaki sposób napisać drugą funkcję która będzie z takiego wzorca pliku XML generować mi tablicę taką jak widać wyżej.

Oto moja funkcja która na podstawie tablicy generuje mi plik xml
  1. function createXML($id)
  2. {
  3. $data = dataXML($id);
  4. $xml = ($id == 0) ? "<?xml version=\"1.0\" encoding=\"utf-8\"?>\r\n<ApplicationInfo>\r\n" : "";
  5. foreach($data as $key=>$value)
  6. {
  7. $xml .= "<".$value['name'].">";
  8. $xml .= (count(createXML($value['id'])) > 0 and $value['value'] == '') ? "\r\n".renderXML($value['id']) : $value['value'];
  9. $xml .= "</".$value['name'].">\r\n";
  10. }
  11. $xml .= ($id == 0) ? "</ApplicationInfo>\r\n" : "";
  12. return $xml;
  13. }


Jednak już nie bardzo mam pomysł jak napisać to w drugą stronę.

plik XML wczytuje za pomocą funkcji simplexml_load_file()
a oto jak wygląda wczytany plik XML
  1. SimpleXMLElement Object
  2. (
  3. [page] => index
  4. [mainMenu] => SimpleXMLElement Object
  5. (
  6. [option] => Array
  7. (
  8. [0] => SimpleXMLElement Object
  9. (
  10. [url] => home
  11. [name] => Strona Główna
  12. )
  13.  
  14. [1] => SimpleXMLElement Object
  15. (
  16. [url] => register
  17. [name] => rejestracja
  18. )
  19.  
  20. )
  21.  
  22. )
  23.  
  24. )
Pyton_000
A powiesz mi czemu takie dziwne rzeczy robisz?
troian
Cytat(Pyton_000 @ 19.09.2017, 11:42:21 ) *
A powiesz mi czemu takie dziwne rzeczy robisz?


cóż jest to część aplikacji, tzn wiele rzeczy zamiast być generowanych z bazy danych jest przechowywana w pliku xml właśnie np szkielet menu czy inne tego typu struktury, cała aplikacja działa bardzo fajnie jednak edycja pliku xml wymusza wejście na serwer i jego ręczną edycję. Napisałem już cały komponent który na podstawie pól formularza generuje mi taką oto tablicę, a z niej jest generowany plik xml. Mam też już napisany skrypt który na podstawie tablicy generuje kod html aby móc dodawać, usuwać lub edytować pola formularza jednak problem sprawa mi konwertowanie pliku XML do postaci tablicy.
Pyton_000
Piszesz sobie funkcję która rekurencyjnie przeleci po wszystkich node.

Wrzucasz cały xml, sprawdzasz 1-szy node. Jeśli ma dzieci to znowu odpalasz funkcję z xml tego node. Jak już nie ma dzieci to dodajesz do tablicy wartości i zwracasz tą wartość.

taki koncept:

  1. $xmlObj = simplexml_load_string($xml);
  2.  
  3. function getTree($xml, $data = []) {
  4. foreach ($xml as $item) {
  5. $childrens = $item->children();
  6. if(count($childrens)) {
  7. array_merge(getTree($childrens, $data), $data);
  8. }
  9. $data[] = '....';
  10. }
  11.  
  12. return $data;
  13. }
  14.  
  15. $array = getTree($xmlObj);
troian
Udało mi się uzyskać efekt taki jaki zamierzałem (prawie) oto kod:
  1. $xmlObj = simplexml_load_file('dot.xml');
  2. class api
  3. {
  4. public $data = [];
  5.  
  6. function getTree($xml,$id,$pid)
  7. {
  8. foreach ($xml as $key=>$item)
  9. {
  10. $childrens = $item->children();
  11. if(count($childrens) > 0)
  12. {
  13. $this->data[] = array('id' => $id, 'parent_id' => $pid, 'name' => $key, 'value' => '');
  14. $this->getTree($childrens, $id+1, $id);
  15. }else{
  16. $this->data[] = array('id' => $id, 'parent_id' => $pid, 'name' => $key, 'value' => (string)$item);
  17. $id++;
  18. }
  19. }
  20. return $this->data;
  21. }
  22. }
  23. $api = new api;
  24. $array = $api->getTree($xmlObj,1,0);


oto co mi zwraca $array
  1.  
  2. (
  3. [0] => Array
  4. (
  5. [id] => 1
  6. [parent_id] => 0
  7. [name] => page
  8. [value] => index
  9. )
  10.  
  11. [1] => Array
  12. (
  13. [id] => 2
  14. [parent_id] => 0
  15. [name] => mainMenu
  16. [value] =>
  17. )
  18.  
  19. [2] => Array
  20. (
  21. [id] => 3
  22. [parent_id] => 2
  23. [name] => option
  24. [value] =>
  25. )
  26.  
  27. [3] => Array
  28. (
  29. [id] => 4
  30. [parent_id] => 3
  31. [name] => url
  32. [value] => home
  33. )
  34.  
  35. [4] => Array
  36. (
  37. [id] => 5
  38. [parent_id] => 3
  39. [name] => name
  40. [value] => Strona Główna
  41. )
  42.  
  43. [5] => Array
  44. (
  45. [id] => 3
  46. [parent_id] => 2
  47. [name] => option
  48. [value] =>
  49. )
  50.  
  51. [6] => Array
  52. (
  53. [id] => 4
  54. [parent_id] => 3
  55. [name] => url
  56. [value] => register
  57. )
  58.  
  59. [7] => Array
  60. (
  61. [id] => 5
  62. [parent_id] => 3
  63. [name] => name
  64. [value] => rejestracja
  65. )
  66.  
  67. )


problem polega na tym że każda kolejna
Array
(
[id] => 3
[parent_id] => 2
[name] => option
[value] =>
)

przyjmuje to samo id czyli powtarza się 2x id 3, a chciałbym uzyskać taki oto efekt
  1.  
  2. (
  3. [0] => Array
  4. (
  5. [id] => 1
  6. [parent_id] => 0
  7. [name] => page
  8. [value] => index
  9. )
  10.  
  11. [1] => Array
  12. (
  13. [id] => 2
  14. [parent_id] => 0
  15. [name] => mainMenu
  16. [value] =>
  17. )
  18.  
  19. [2] => Array
  20. (
  21. [id] => 3
  22. [parent_id] => 2
  23. [name] => option
  24. [value] =>
  25. )
  26.  
  27. [3] => Array
  28. (
  29. [id] => 4
  30. [parent_id] => 3
  31. [name] => url
  32. [value] => home
  33. )
  34.  
  35. [4] => Array
  36. (
  37. [id] => 5
  38. [parent_id] => 3
  39. [name] => name
  40. [value] => Strona Główna
  41. )
  42.  
  43. [5] => Array
  44. (
  45. [id] => 6
  46. [parent_id] => 2
  47. [name] => option
  48. [value] =>
  49. )
  50.  
  51. [6] => Array
  52. (
  53. [id] => 7
  54. [parent_id] => 6
  55. [name] => url
  56. [value] => register
  57. )
  58.  
  59. [7] => Array
  60. (
  61. [id] => 8
  62. [parent_id] => 6
  63. [name] => name
  64. [value] => rejestracja
  65. )
  66.  
  67. )


Czy ktoś ma może jakiś pomysł jak to rozwiązać?
Puszy
Możesz skorzystać z serializera Symfony: https://symfony.com/doc/current/components/serializer.html

Paczka dostępna w Composerze: https://packagist.org/packages/symfony/serializer
Pyton_000
Ustaw $id jako referencję (&$id)
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.