Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> [XML][PHP]W jaki sposób pobrać wartość z pliku XML?
trifek
post 23.09.2018, 15:34:02
Post #1





Grupa: Zarejestrowani
Postów: 340
Pomógł: 0
Dołączył: 28.09.2015

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


Witam,
Mam taki obiekt:

  1. @attributes] => Array
  2. (
  3. [version] => 1.2
  4. )
  5.  
  6. [file] => SimpleXMLElement Object
  7. (
  8. [@attributes] => Array
  9. (
  10. [source-language] => en
  11. [target-language] => pl
  12. [datatype] => plaintext
  13. [original] => file.ext
  14. )
  15.  
  16. [body] => SimpleXMLElement Object
  17. (
  18. [trans-unit] => Array
  19. (
  20. [0] => SimpleXMLElement Object
  21. (
  22. [@attributes] => Array
  23. (
  24. [id] => note
  25. )
  26.  
  27. [source] => note
  28. [target] => UWAGA
  29. )
  30.  
  31. [1] => SimpleXMLElement Object
  32. (
  33. [@attributes] => Array
  34. (
  35. [id] => tip
  36. )
  37.  
  38. [source] => tip
  39. [target] => PORADA
  40. )
  41.  
  42.  
  43. [66] => SimpleXMLElement Object
  44. (
  45. [@attributes] => Array
  46. (
  47. [id] => help.more_information
  48. )
  49.  
  50. [source] => help.more_information
  51. [target] => SimpleXMLElement Object
  52. (
  53. )
  54.  
  55. )
  56.  
  57. )
  58.  
  59. )
  60.  
  61. )
  62.  





i funkcję w php:

  1. public function openFileWithTranslations(string $tagId)
  2. {
  3. $filePath = "../pl.xlf";
  4. if (file_exists($filePath)) {
  5. $objectFromXML = simplexml_load_file($filePath);
  6. $vel = 'trans-unit';
  7. foreach ($objectFromXML->$vel as $msg)
  8. {
  9. return $msg['id'];
  10. }
  11. }
  12. }
  13.  


Chciałbym aby po otwarciu funkcji openFileWithTranslations xml był parsowany:

1. note => zwróciłby mi wynik: Uwaga
2. tip => zwróciłby mi wynik: porada

itp.

Chciałbym otrzymywać w oparciu o podany klucz tagId wartość z parametru target.

Taki debug:

  1. $objectFromXML = simplexml_load_file($filePath);
  2. $vel = 'trans-unit';
  3. echo "@".print_r($objectFromXML->$vel) ;
  4.  


Pokazuje tylko SimpleXMLElement Object ( ) @1.

Z kolei taki:


  1. echo "@".print_r($objectFromXML)
  2. SHow all object.
  3.  

Może tu jest gdzieś problem?

Wie ktoś może jak to naprawić?
Go to the top of the page
+Quote Post
djjbenus
post 26.09.2018, 14:25:02
Post #2





Grupa: Zarejestrowani
Postów: 71
Pomógł: 0
Dołączył: 22.05.2010

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


Z tego co widzę to w ogóle nie szukasz wartości o której piszesz ('note' czy 'tip').
Przede wszystkim masz obiekty w obiekcie więc nie możesz się odwołać do nich jak w tablicy.
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: 28.03.2024 - 12:49