0

I have been playing with parsing XML in PHP . I have tested multiple examples on the web but perhaps there has been a change in the way this works with different PHP versions?

I tested the examples here: https://www.w3schools.com/php/php_xml_simplexml_read.asp I copied and pasted from their "working" examples in "Run Example" but it does not load!

NONE seem to work for me at least in my version of PHP (PHP 7.0.33-0+deb9u10)

Aside from that when I previously tried to test and had some success the issue I had was when the XML contained common field names like

<scan:DiscreteResolution>
<scan:XResolution>100</scan:XResolution>
<scan:YResolution>100</scan:YResolution>
</scan:DiscreteResolution>
<scan:DiscreteResolution>
<scan:XResolution>150</scan:XResolution>
<scan:YResolution>150</scan:YResolution>
</scan:DiscreteResolution>

As you can see above many start and end tags are repeated.

So may I ask simply what does work?

  • Can you add the PHP you have tried and a full sample of the XML ( from the very start - not necessarily all of it )? The above has a namespace to deal with - have you factored that into your code? – Professor Abronsius Aug 11 '21 at 07:29
  • What errors did you encounter when you used simplexml_load_string()? And I don't think you pasted the entire XML because you used namespaces but did not define them. – Sibi Kandathil Aug 11 '21 at 07:30
  • 1
    SimpleXML doesn’t handles XML namespaces all too well, I’d really recommend that you use the DOM extension instead. (You _can_ work with namespaces with SimpleXML, see [Parse XML with Namespace using SimpleXML](https://stackoverflow.com/q/595946/1427878), but it is rather complex IMHO.) – CBroe Aug 11 '21 at 09:38
  • Your XML sample doesn't have two things to make it a well-formed XML: (1) a root node is missing, (2) namespace declaration is missing. – Yitzhak Khabinsky Aug 11 '21 at 13:22
  • The most common error I get is: Uncaught Error: Call to undefined function simplexml_load_string() this seems to say that there is no such function! This is on a Raspberry Pi – Tekno Geekz Aug 11 '21 at 15:57
  • Just tried on xampp windows with php 8 and it works there! Something with PHP on Raspberry Pi that does not have xml parser??? – Tekno Geekz Aug 11 '21 at 16:15

0 Answers0