Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [PHP] Parsowanie XML
lled3
post 6.09.2018, 21:31:05
Post #1





Grupa: Zarejestrowani
Postów: 91
Pomógł: 0
Dołączył: 7.12.2007

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


Taki kod działa:
  1.  
  2. <?php
  3.  
  4. $string = "
  5. <response>
  6. <status>OK</status>
  7. <trans>
  8. <cancel></cancel>
  9. <auth_fraud>0</auth_fraud>
  10. <ts>15362231</ts>
  11. <add_client_city></add_client_city>
  12. </trans>
  13. </response>
  14. ";
  15.  
  16.  
  17. $string = <<<XML
  18.  
  19. $string
  20.  
  21. XML;
  22.  
  23.  
  24. $xml = simplexml_load_string($string);
  25.  
  26. $parts[5] = $xml->trans->status;
  27.  
  28.  
  29. echo $parts[5];
  30.  
  31.  
  32. ?>
  33.  


A taki kod już nie ...

  1.  
  2. <?php
  3.  
  4. $string = "
  5. <?xml version=\"1.0\" encoding=\"ISO-8859-2\"?>
  6. <response>
  7. <status>OK</status>
  8. <trans>
  9. <cancel></cancel>
  10. <auth_fraud>0</auth_fraud>
  11. <ts>15362231</ts>
  12. <add_client_city></add_client_city>
  13. </trans>
  14. </response>
  15. ";
  16.  
  17.  
  18.  
  19. $xml = simplexml_load_string($string);
  20.  
  21. $parts[5] = $xml->trans->status;
  22.  
  23.  
  24. echo $parts[5];
  25.  
  26.  
  27. ?>


Próba zastosowania <<<XML $string XML; dla drugiego kodu, wywala informację "XML declaration allowed only at the start of the document".

Moja zmienna string zawiera "<?xml version=\"1.0\" encoding=\"ISO-8859-2\"?>" - zatem jak zmienić kod, by reszta zadziała? Gdzie tkwi błąd?
Go to the top of the page
+Quote Post
viking
post 7.09.2018, 05:40:43
Post #2





Grupa: Zarejestrowani
Postów: 6 365
Pomógł: 1114
Dołączył: 30.08.2006

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


Włącz raportowanie błędów, usuń entery z początku. $parts[5] = $xml->status; jak już.


--------------------
Go to the top of the page
+Quote Post
lled3
post 7.09.2018, 20:33:08
Post #3





Grupa: Zarejestrowani
Postów: 91
Pomógł: 0
Dołączył: 7.12.2007

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


Teraz wywala błąd:

Warning: simplexml_load_string(): Entity: line 1: parser error : Start tag expected, '<' not found in /home/vps/pub/raport_ok.php on line 93
Go to the top of the page
+Quote Post
viking
post 8.09.2018, 05:07:41
Post #4





Grupa: Zarejestrowani
Postów: 6 365
Pomógł: 1114
Dołączył: 30.08.2006

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


Mówiłem Ci. Usuń entery.


--------------------
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: 18.04.2024 - 09:51