I am trying to find a way to iterate through an XML recordset containing a namespace. However, I don’t know the field names in advance. Sample XML is below.
<?xml version="1.0" encoding="utf-8"?>
<string xmlns="http://www.site.com/SMART/Updates">
<NewDataSet>
<record>
<FIELD1>data1</FIELD1>
<FIELD2>data2</FIELD2>
<FIELD3>data3</FIELD3>
</record>
<record>
<FIELD1>data1</FIELD1>
<FIELD2>data2</FIELD2>
<FIELD3>data3</FIELD3>
</record>
</NewDataSet>
Again, I won’t know the field names in advance. I need read the namespace, find the name of the root element (“NewDataSet”, in this case) and then need to get the field names and values of the individual elements. I have tried to use $xml->getname(), and $xml->xpath(‘\’) to find the root element name, but been unable to crack it.
Plain DOM functions are the best way to process XML.
Demo or code:
Output: