Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> Generowanie tablicy z pliku wsadowego XML, Generowanie tablicy na podstawie pliku xml
troian
post
Post #1





Grupa: Zarejestrowani
Postów: 184
Pomógł: 2
Dołączył: 3.02.2013

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


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. )
Go to the top of the page
+Quote Post
 
Start new topic
Odpowiedzi (1 - 6)
Pyton_000
post
Post #2





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

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


A powiesz mi czemu takie dziwne rzeczy robisz?
Go to the top of the page
+Quote Post
troian
post
Post #3





Grupa: Zarejestrowani
Postów: 184
Pomógł: 2
Dołączył: 3.02.2013

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


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.
Go to the top of the page
+Quote Post
Pyton_000
post
Post #4





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

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


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);
Go to the top of the page
+Quote Post
troian
post
Post #5





Grupa: Zarejestrowani
Postów: 184
Pomógł: 2
Dołączył: 3.02.2013

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


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ć?

Ten post edytował troian 19.09.2017, 12:26:02
Go to the top of the page
+Quote Post
Puszy
post
Post #6





Grupa: Zarejestrowani
Postów: 279
Pomógł: 42
Dołączył: 10.10.2011

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


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
Go to the top of the page
+Quote Post
Pyton_000
post
Post #7





Grupa: Zarejestrowani
Postów: 8 068
Pomógł: 1414
Dołączył: 26.10.2005

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


Ustaw $id jako referencję (&$id)
Go to the top of the page
+Quote Post

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

 



RSS Aktualny czas: 20.08.2025 - 10:00