Background
I am trying to extract the value of the following element
<D:POD>2021-08-07</D:POD>
from the following XML.
The output I am trying to get
2021-08-07
The problem of this XML is that its elements (including the root element) contain namespaces. I am struggling to deal with this using simplexml.
My steps
I request and receive the above XML from the api using
$this->curl = curl_init($this->url);
curl_setopt($this->curl, CURLOPT_URL, $this->url);
curl_setopt($this->curl, CURLOPT_RETURNTRANSFER, true);
$this->headers = array(
"Accept: application/xml",
);
curl_setopt($this->curl, CURLOPT_HTTPHEADER, $this->headers);
$this->resp = curl_exec($this->curl);
curl_close($this->curl);
Then I run simplex_load_string:
$this->xml = simplexml_load_string($this->resp);
I am trying to access:
<are:Ares_odpovedi>(root)
<are:Odpoved>
<D:Vypis_OR>
<D:ZAU>
-> <D:POD>2021-08-07</D:POD>
I am not able to point to the element <D:POD> with the standard $xml->element_lvl1->element_lvl2… since all the elements have namespaces.
I tried running print_r on the result to help me to point to these namespaced elements:
print_r($this->xml);
but it only displays the attributes of the root element (are:Ares_odpovedi) in the XMLobject. I cannot see any child elements of this root element.
SimpleXMLElement Object ( [@attributes] => Array ( [odpoved_datum_cas]
=> 2022-01-17T11:41:46 [odpoved_pocet] => 1 [odpoved_typ] => Vypis_OR [vystup_format] => XML [xslt] => klient [validation_XSLT] =>
http://wwwinfo.mfcr.cz/ares/xml_doc/schemas/ares/ares_odpovedi.xsl
[Id] => ares ) )
Question:
Is there please a way how to get the value of a specific child element (<D:POD>2021-08-07</D:POD>) in this xml where all the elements (including the root element) contain namespaces?