Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> Ekport pliku xml do Tablicy, problem z kluczami
marcio
post 6.04.2012, 14:36:40
Post #1





Grupa: Zarejestrowani
Postów: 2 291
Pomógł: 156
Dołączył: 23.09.2007
Skąd: ITALY-MILAN

Ostrzeżenie: (10%)
X----


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?


--------------------
Zainteresowania: XML | PHP | MY(SQL)| C# for .NET | PYTHON
http://code.google.com/p/form-builider/
Moj blog
Go to the top of the page
+Quote Post
skowron-line
post 6.04.2012, 15:09:10
Post #2





Grupa: Zarejestrowani
Postów: 4 340
Pomógł: 542
Dołączył: 15.01.2006
Skąd: Olsztyn/Warszawa

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


Może
http://php.net/manual/en/class.arrayiterator.php


--------------------
I'm so fast that last night I turned off the light switch in my hotel room and was in bed before the room was dark - Muhammad Ali.
Peg jeżeli chcesz uprawiać sex to dzieci muszą wyjść, a jeżeli chcesz żeby był dobry ty też musisz wyjść - Al Bundy.

QueryBuilder, Mootools.net, bbcradio1::MistaJam
http://www.phpbench.com/
Go to the top of the page
+Quote Post
marcio
post 6.04.2012, 19:31:38
Post #3





Grupa: Zarejestrowani
Postów: 2 291
Pomógł: 156
Dołączył: 23.09.2007
Skąd: ITALY-MILAN

Ostrzeżenie: (10%)
X----


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


--------------------
Zainteresowania: XML | PHP | MY(SQL)| C# for .NET | PYTHON
http://code.google.com/p/form-builider/
Moj blog
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 Wersja Lo-Fi Aktualny czas: 19.06.2025 - 10:20