Pomoc - Szukaj - Użytkownicy - Kalendarz
Pełna wersja: [XML][PHP]parsowanie xml do tablicy asocjacyjnej
Forum PHP.pl > Forum > Przedszkole
Wasper
Hej.
Jako ze z problemem samego xmla sobie poradzilem teraz przyszla pora na parser.
Najgorsze jest to, ze pliki ktore parsuje maja zroznicowana strukture i niektore dzialaja a niektore nie.

Moze umieszcze to, co do tej pory udalo mi sie zdzialac.

  1. <?php
  2.  
  3. class XML {
  4. public function __construct() {
  5. }
  6.  
  7. public function setHTTP($httpAddress) {
  8. $this->httpAddress = $httpAddress;
  9. }
  10.  
  11. private function getHTTP() {
  12. return $this->httpAddress;
  13. }
  14.  
  15. public function authorisation($user, $password) {
  16. $this->user = $user;
  17. $this->password = $password;
  18. }
  19.  
  20. private function getUser() {
  21. return $this->user;
  22. }
  23.  
  24. private function getPassword() {
  25. return $this->password;
  26. }
  27.  
  28. public function addParameter($key, $value) {
  29. $this->parameters[$key] = $value;
  30. }
  31.  
  32. private function getParameters() {
  33. return $this->parameters;
  34. }
  35.  
  36. private function parametersPart() {
  37. $first = true;
  38. $outString = "";
  39. $parameters = $this->getParameters();
  40. if(!empty($parameters)) {
  41. foreach($parameters as $key=>$value) {
  42. if($first) {
  43. $outString .= "?".$key."=".$value;
  44. $first = false;
  45. } else {
  46. $outString .= "&".$key."=".$value;
  47. }
  48. }
  49. }
  50.  
  51. return $outString;
  52. }
  53.  
  54. private function getFullUrl() {
  55. $parameters = $this->parametersPart();
  56. if(!empty($parameters)) {
  57. return $this->getHTTP().$this->parametersPart();
  58. } else {
  59. return $this->getHTTP();
  60. }
  61. }
  62.  
  63. public function getXml() {
  64. $xml = curl_init();
  65. curl_setopt($xml, CURLOPT_URL, $this->getFullUrl());
  66. curl_setopt($xml, CURLOPT_RETURNTRANSFER, true);
  67. curl_setopt($xml, CURLOPT_USERPWD, $this->getUser().":".$this->getPassword());
  68. curl_setopt($xml, CURLOPT_HTTPAUTH, CURLAUTH_BASIC);
  69. $output = curl_exec($xml);
  70. curl_close($xml);
  71.  
  72. return simplexml_load_string($output);
  73. }
  74.  
  75. public function objectToArray($object) {
  76. $arrayOb = get_object_vars($object);
  77. return $arrayOb;
  78. }
  79.  
  80. public function finalArray() {
  81. $values = array();
  82. $mainArray = $this->objectToArray($this->getXml());
  83.  
  84. foreach($mainArray as $key=>$value) {
  85. if(is_object($value)) {
  86. foreach($this->objectToArray($value) as $k=>$v) {
  87. if(is_object($v)) {
  88. foreach($this->objectToArray($v) as $ka=>$va) {
  89. //echo "---- K: ".$ka." , V: ".$va."<BR>";
  90. }
  91. } else if(is_array($v)){
  92. foreach($v as $ka=>$va) {
  93. //echo "---- K: ".$key." , V: ".$value."<BR>";
  94. }
  95. } else {
  96. //echo "-- K: ".$key." , V: ".$value."<BR>";
  97. }
  98. }
  99. } else if(is_array($value)){
  100. foreach($value as $k=>$v) {
  101. if(is_object($v)) {
  102. foreach($this->objectToArray($v) as $ka=>$va) {
  103. //echo "---- K: ".$ka." , V: ".$va."<BR>";
  104. }
  105. } else if(is_array($v)){
  106. foreach($v as $ka=>$va) {
  107. //echo "---- K: ".$key." , V: ".$value."<BR>";
  108. }
  109. } else {
  110. //echo "-- K: ".$key." , V: ".$value."<BR>";
  111. }
  112. }
  113. } else {
  114. $values[$key] = $value;
  115. //echo "K: ".$key." , V: ".$value."<BR>";
  116. }
  117. }
  118.  
  119.  
  120. return $values;
  121. }
  122.  
  123. public function getValues() {
  124. return $this->finalArray();
  125. }
  126.  
  127.  
  128. public $httpAddress;
  129. public $user;
  130. public $password;
  131. public $parameters = array();
  132.  
  133.  
  134. }
  135. ?>


(echo jest typowo pogladowo oczywiscie tongue.gif )

i teraz np jeden zrzut otrzymuje w miare dobrze:

XML

  1. <award-categories type="array">
  2. <award-category>
  3. <id type="integer">340</id>
  4. <name>Po meczu</name>
  5. </award-category>
  6. <award-category>
  7. <id type="integer">341</id>
  8. <name>Na wyjeździe</name>
  9. </award-category>
  10. <award-category>
  11. <id type="integer">342</id>
  12. <name>Trofea smakoszy</name>
  13. </award-category><award-category>
  14. <id type="integer">343</id>
  15. <name>Dla zawodowców</name>
  16. </award-category>
  17. </award-categories>


wynik na ekranie

  1. -- K: @attributes , V: Array
  2. ---- K: id , V: 340
  3. ---- K: name , V: Po meczu
  4. ---- K: id , V: 341
  5. ---- K: name , V: Na wyjeździe
  6. ---- K: id , V: 342
  7. ---- K: name , V: Trofea smakoszy
  8. ---- K: id , V: 343
  9. ---- K: name , V: Dla zawodowców


a inny na tym samym schemacie sie krzaczy:

XML

  1. <product-award>
  2. <available-id type="NilClass">2</available-id>
  3. <award-type-id type="NilClass">2</award-type-id>
  4. <can-be-minus-id type="NilClass">1</can-be-minus-id>
  5. <catalog-code type="NilClass">B15</catalog-code>
  6. <catalog-name type="NilClass">Zestaw bitewny Clone Trooper LEGO</catalog-name>
  7. <id type="integer">12352</id>
  8. <min-quantity-for-order type="NilClass">1</min-quantity-for-order>
  9. <point-value-for-catalog type="NilClass">770</point-value-for-catalog>
  10. </product-award>


wynik na ekranie

  1. -- K: @attributes , V: Array
  2. ---- K: @attributes , V: Array
  3. -- K: head , V:
  4. -- K: head , V:
  5. ---- K: @attributes , V: Array
  6. ---- K: h1 , V: We're sorry, but something went wrong.
  7. ---- K: p , V: We've been notified about this issue and we'll take a look at it shortly.


Wychodzi na to, ze powinienem wejsc jeszcze glebiej w strukture... ale moznaby to bylo w takim razie mnozyc i mnozyc...
zastanawialem sie nad jakas funkcja ktora bedzie zapetlac warunki az do ostatniego poziomu obiektu.. ale jeszcze nie wymyslilem

Bede wdzieczny za jakies przemyslenia, opinie, propozycje i wskazowki.
Nie chcialbym skonczyc temu na tekscie "bo w internecie jest tego pelno".. moze i jest, ale forum tez jest po to, zeby uzyskiwac informacje.
Pozdro
Wasp
jaslanin
http://www.bin-co.com/php/scripts/xml2array/
http://stackoverflow.com/questions/6578084...6578400#6578400

Ale zwykle output bardziej złożonych XMLi np. z przestrzeniami nazw itp jest beznadziejny. I bardziej się opłaca ręcznie przetwarzać XML za pomocą SimpleXMLelement
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.