I am fetching a large block of XML data from a MySQL Database and attempting to parse out the relevant information. There is an ArrayOfString
which I have never seen before, however I need to get the values out of it for use.
I have tried several methods including SimpleXML's __ToString()
, casting it with (string)
and so on but no luck. How can I properly convert this to either a string that I can explode into array, or just to an array?
$game_ids = $machine_xml->add[25]->attributes()->value[0];
var_dump($game_ids);
Results:
object(SimpleXMLElement)#2 (1) {
[0]=>
string(331) "<?xml version="1.0" encoding="utf-16"?>
<ArrayOfString xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<string>7</string>
<string>46</string>
<string>4</string>
<string>60</string>
<string>5</string>
<string>23</string>
<string>50</string>
</ArrayOfString>"
}
Firstly, you seem to have XML wrapped in XML. That is, your XML document looks, in part, like this:
So the first thing you need to do is get that inner piece of XML, and parse it with SimpleXML:
Now, you have a fairly straight-forward XML document: a single
<ArrayOfString>
element containing a series of<string>
elements. So you can just loop over them with aforeach
statement, and collect into an array: