I have a php application and I am trying to echo a atrribute from the xml.
Here is the xml:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?> <header xmlns="xxx:xxx:xxxx:xxx:respUploadFisier:v1" dateResponse="202202" ExecutionStatus="0" index="206"/>
This is what I have tried till now:
xml = simplexml_load_string($result2); foreach($xml->header[3]->attributes() as $a => $b) { echo $a,'="',$b,""n"; }
But it gives me this errors:
Warning: simplexml_load_string(): Entity: line 1: parser error : Start tag expected, '<' not found in C:xampphtdocsefacturaverificastarea.php on line 80 Warning: simplexml_load_string(): <?xml version="1.0" encoding="UTF- 8" standalone="yes in C:xampphtdocsefacturaverificastarea.php on line 80 Warning: simplexml_load_string(): ^ in C:xampphtdocsefacturaverificastarea.php on line 80 Notice: Trying to get property 'header' of non-object in C:xampphtdocsfactverificastarea.php on line 81 Notice: Trying to access array offset on value of type null in C:xampphtdocsfactverificastarea.php on line 81 Fatal error: Uncaught Error: Call to a member function attributes() on null in C:xampphtdocsfactverificastarea.php:81 Stack trace: #0 {main} thrown in C:xampphtdocsfactverificastarea.php on line 81
Can someone give me an example to extract from the xml from the beginning of the post the index ? Thanks in advance. I am a newbie in php
Advertisement
Answer
To get a specific attribute you can try like this :
$xml=simplexml_load_string($myXMLData) or die("Error: Cannot create object"); function xml_attribute($object, $attribute) { if(isset($object[$attribute])) return (string) $object[$attribute]; } print_r(xml_attribute($xml,'dateResponse'));