I am working with PHP and XPath connecting to a remote XML based API. A sample response from the server is as this one below.
<OTA_PingRS>
<Success />
<EchoData>This is some test data</EchoData>
</OTA_PingRS>
You can see there is no starting tag <Success> so how do I search for the existance of <Success /> using Xpath?
Thanks
Simon
The
<Success />element is an empty element, meaning it has no value. It is both, Start and End Tag.You can test for existence of nodes with the XPath function
boolean()To do that with
DOMXPathyou need to use theDOMXPath::evaluate()method because it will return a typed result, in this case aboolean:demo
Of course, you can also just query for
/OTA_PingRS/Successand see whether there are results in the returnedDOMNodeList:demo
You can also use SimpleXML: