Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

> xpath dla xml brak poprawnego zwrotu
rafaliex
post 24.09.2022, 14:25:09
Post #1





Grupa: Zarejestrowani
Postów: 9
Pomógł: 0
Dołączył: 21.08.2010

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


Mam komunikację za urządzeniem po api, i motoda komunikacyjna zwraca treść w postaci xmla jako zwrot dla requestu.
Problem w tym że mam mały problem z wyciągnięciem danych.

Zwrot taki wygląda tak:

  1. <?xml version="1.0"?>
  2. <env:Envelope
  3. xmlns:env="http://www.w3.org/2003/05/soap-envelope">
  4. <env:Body>
  5. <getAdminConfigResponse
  6. xmlns="http://www.kostac.com/automation/v1">
  7. <code>100</code>
  8. <message>OK</message>
  9. <adminConfigList>
  10. <adminConfigListEntry>
  11. <slotId>2</slotId>
  12. <dataPort>
  13. <ipAddress>172.28.0.9</ipAddress>
  14. <gatewayAddress>172.28.0.10</gatewayAddress>
  15. <subnetMask>255.255.255.252</subnetMask>
  16. <ipv6AddressAuto>N/A</ipv6AddressAuto>
  17. <enable>1</enable>
  18. <autoNegotiation>1</autoNegotiation>
  19. <linkSpeed>AUTO</linkSpeed>
  20. <currentLinkSpeed>1000</currentLinkSpeed>
  21. <connectorType>optical</connectorType>
  22. <vlanList/>
  23. <icmp>1</icmp>
  24. </dataPort>
  25. <dataPortB>
  26. <ipAddress>172.30.8.1</ipAddress>
  27. <gatewayAddress>172.30.8.2</gatewayAddress>
  28. <subnetMask>255.255.252.0</subnetMask>
  29. <ipv6AddressAuto>N/A</ipv6AddressAuto>
  30. <enable>1</enable>
  31. <autoNegotiation>1</autoNegotiation>
  32. <linkSpeed>AUTO</linkSpeed>
  33. <currentLinkSpeed>1000</currentLinkSpeed>
  34. <connectorType>optical</connectorType>
  35. <vlanList/>
  36. <icmp>0</icmp>
  37. </dataPortB>
  38. </adminConfigListEntry>
  39. </adminConfigList>
  40. </getAdminConfigResponse>
  41. </env:Body>
  42. </env:Envelope>


I teraz chciałem wyciągnąć: "<ipAddress>172.30.8.1</ipAddress>" a dokładniej zawartość tego elementu.

Przygotowałem sobie coś takiego:

  1. $xml_ip_address = '<?xml version="1.0"?>
  2. <env:Envelope
  3. xmlns:env="http://www.w3.org/2003/05/soap-envelope">
  4. <env:Body>
  5. <getAdminConfigResponse
  6. xmlns="http://www.kostac.com/automation/v1">
  7. <code>100</code>
  8. <message>OK</message>
  9. <adminConfigList>
  10. <adminConfigListEntry>
  11. <slotId>2</slotId>
  12. <dataPort>
  13. <ipAddress>172.28.0.9</ipAddress>
  14. <gatewayAddress>172.28.0.10</gatewayAddress>
  15. <subnetMask>255.255.255.252</subnetMask>
  16. <ipv6AddressAuto>N/A</ipv6AddressAuto>
  17. <enable>1</enable>
  18. <autoNegotiation>1</autoNegotiation>
  19. <linkSpeed>AUTO</linkSpeed>
  20. <currentLinkSpeed>1000</currentLinkSpeed>
  21. <connectorType>optical</connectorType>
  22. <vlanList/>
  23. <icmp>1</icmp>
  24. </dataPort>
  25. <dataPortB>
  26. <ipAddress>172.30.8.1</ipAddress>
  27. <gatewayAddress>172.30.8.2</gatewayAddress>
  28. <subnetMask>255.255.252.0</subnetMask>
  29. <ipv6AddressAuto>N/A</ipv6AddressAuto>
  30. <enable>1</enable>
  31. <autoNegotiation>1</autoNegotiation>
  32. <linkSpeed>AUTO</linkSpeed>
  33. <currentLinkSpeed>1000</currentLinkSpeed>
  34. <connectorType>optical</connectorType>
  35. <vlanList/>
  36. <icmp>0</icmp>
  37. </dataPortB>
  38. </adminConfigListEntry>
  39. </adminConfigList>
  40. </getAdminConfigResponse>
  41. </env:Body>
  42. </env:Envelope>
  43. ';
  44.  
  45. $xml = simplexml_load_string($xml_ip_address);
  46. $programme_with_xmlid = $xml->xpath("/env:Envelope/env:Body/getAdminConfigResponse/adminConfigList/adminConfigListEntry/dataPortB/ipAddress");
  47.  
  48. var_dump($programme_with_xmlid);


Próbowałem różnych xpathów:

  1. /env:Envelope/env:Body/getAdminConfigResponse/adminConfigList/adminConfigListEntry/dataPortB/ipAddress

  1. //dataPortB[1]/ipAddress[1]/text()[1]

I też kilku innych ale nigdy nie udało mi się zwrócić wyników.
Gdy na jakiś generatorach online próbuje wyciągnąć takową wartość to niby te xpathy są podobne do moich ale już w php nie działają.

Czy może ktoś mi uświadomić gdzie jest błąd albo podpowiedzieć coś?

Bo już próbowałem aboslutnej ścieżki jak i relatywnej a i tak nic nie działa.
Go to the top of the page
+Quote Post

Posty w temacie


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: 15.06.2024 - 05:31