Witaj Gościu! ( Zaloguj | Rejestruj )

Forum PHP.pl

 
Reply to this topicStart new topic
> 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
kreatiff
post 24.09.2022, 14:58:30
Post #2





Grupa: Zarejestrowani
Postów: 324
Pomógł: 105
Dołączył: 7.08.2012

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


To wygląda na problem z namespace.
Sprawdź 1. i 2. komentarz: https://www.php.net/manual/en/simplexmlelement.xpath.php

A samo zapytanie xpath można skrócić do "//ipAddress".
Go to the top of the page
+Quote Post
rafaliex
post 24.09.2022, 15:33:37
Post #3





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

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


Ok komentarz z dokumentacji pomógł, problem rzeczywiście z namespace.
Kolejnym problemem że maszyna która jest u klienta ma stary system i starego php i musi tam być jakiś bug.
Nawet po str_replace na wersji 5.5.9 tego xpath nie działa. Dopiero od 5.5.30 w górę jest zwracany wynik.

Ale to już problem samego php, a za pomoc dzięki.
Go to the top of the page
+Quote Post
trueblue
post 24.09.2022, 18:46:10
Post #4





Grupa: Zarejestrowani
Postów: 6 761
Pomógł: 1822
Dołączył: 11.03.2014

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


  1. $xml_ip_address = str_replace('xmlns=', 'ns=', $xml_ip_address);
  2.  
  3. $doc = new DOMDocument();
  4. $doc->loadXML($xml_ip_address);
  5.  
  6. $xpath = new DOMXpath($doc);
  7. $xpath->registerNamespace("env", "http://www.w3.org/2003/05/soap-envelope");
  8. echo $xpath->evaluate('string(//adminConfigList/adminConfigListEntry/dataPortB/ipAddress)');


--------------------
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: 29.03.2024 - 13:35