Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: Ekport pliku xml do Tablicy
Forum PHP.pl > Forum > PHP
marcio
Hej mam male pytanie mam taka oto tablice:
  1. $configs['plugins_settings'] = array(
  2. 'compatible' => array('comment', 'polling'),
  3. 'after' => array(
  4. 'readNews' => array(
  5. 'description' => 'Czytamy news-a',
  6. 'plugins' => array(
  7. 'comment' => array(
  8. 'settings' => array('segment_ref' => 0),
  9. 'actions' => array('submit', 'delete')
  10. )
  11. )
  12. ),
  13. 'delete' => array(
  14. 'description' => 'Usuwamy news-a',
  15. 'plugins' => array(
  16. 'comment' => array(
  17. 'settings' => array('segment_ref' => 3),
  18. 'actions' => array('submit', 'delete')
  19. )
  20. )
  21. )
  22. )
  23. );

A w xml mam to tak:
  1. <?xml version="1.0" encoding="UTF-8"?>
  2. <config>
  3. <plugins_settings>
  4. <compatible>
  5. <plugin>comment</plugin>
  6. <plugin>polling</plugin>
  7. </compatible>
  8. <after>
  9. <readNews>
  10. <description>Czytamy news-a</description>
  11. <plugins>
  12. <comment>
  13. <settings>
  14. <segment_ref>0</segment_ref>
  15. <actions>
  16. <action>submit</action>
  17. <action>delete</action>
  18. </actions>
  19. </settings>
  20. </comment>
  21. </plugins>
  22. </readNews>
  23. </after>
  24. </plugins_settings>
  25. </config>

I teraz chodzi mi dokladnie o sekcje <plugin></plugin> i <action></action>
I mam taki kod ktory to daje do tablicy php:
  1. <?php
  2.  
  3. /**
  4. *Volta framework
  5.  
  6. *@author marcio <opi14@op.pl>, <polishvodka7@gmail.com>
  7. *@copyright Copyright (c) 2012, marcio
  8. *@version 1.0
  9. */
  10.  
  11. require_once(DIR_INTERFACES.'IConfig.php');
  12.  
  13. class Vf_Config_Xml_Adapter_Exception extends Exception { }
  14.  
  15. class Vf_Config_Xml_Adapter implements IConfig
  16. {
  17.  
  18. public function load($config_path)
  19. {
  20. if(file_exists($config_path))
  21. {
  22. $xml = new SimpleXMLElement($config_path, 0, true);
  23. return $this -> _XmlToArray($xml);
  24. }
  25. else
  26. {
  27. throw new Vf_Config_Xml_Adapter_Exception("Nie zaladowano konfiguracji: ".$config_path);
  28. }
  29. }
  30.  
  31.  
  32. public function isAcceptSuffix($suffix)
  33. {
  34. if($suffix == 'xml')
  35. return true;
  36. return false;
  37. }
  38.  
  39.  
  40. private function _XmlToArray($xml_object)
  41. {
  42. $config = array();
  43. if(is_object($xml_object) && $vars = get_object_vars($xml_object))
  44. {
  45. foreach($vars as $key => $value)
  46. {
  47. if(!is_object($value))
  48. {
  49. $config[$key] = $value;
  50. }
  51. else
  52. {
  53. $config[$key] = $this -> _XmlToArray($value);
  54. }
  55. }
  56. return $config;
  57. }
  58. return $xml_object;
  59. }
  60. }
  61. ?>

Rezultat jest dobry oprocz tych 2 sekcji zwraca mi(czesc tablicy zwiazana tylko z tymi sekcjami):
Cytat
Array ( [plugin] => Array ( [0] => comment [1] => polling )
Array ( [action] => Array ( [0] => submit [1] => delete ) )

A chcialbym miec:
Cytat
Array ( [0] => comment [1] => polling )
Array ( [0] => submit [1] => delete )

A dodaje mi nazwe seksji i potem tablice.
Myslalem to zrobic na 2 sposoby:
1)Sprawdzac czy dana sekja ma wiecej elementow jesli tak iterowac po niej i dawac $key => $value do tablicy
2)Zamiast:
  1. <plugin>comment</plugin>
  2. <plugin>polling</plugin>

Zrobic:
  1. <plugin key="0">comment</plugin>
  2. <plugin key="1">polling</plugin>

I czytac wartosci atrybutu key

Czy idzie to zrobic w prostszy sposob?
skowron-line
Może
http://php.net/manual/en/class.arrayiterator.php
marcio
Jest http://www.php.net/manual/en/class.simplexmliterator.php ale zabardzo nie wiem czy tak jak to chce zrobic bedzie poprawnie

Nie wazne przegladalem zrodla przynajmniiej 10-ciu roznych fw i kazdy laduje xml tak jak to mialem na poczatku nie wazne, poprawilem klase jesli kogos interesuje dziala jak kazda inna + parsuje do tablicy tak jak ja potrzebuje te sekcje ktore potrzebuje.
http://pyp.net.pl/source/4f7f35c2515fb
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.