Reputation: 14774
I have a string variable, containing XML data and I was wondering if there is an easy way to extract fields and data from the file. Through an iterative generic way, without specifying name of the tags in the code.
Upvotes: 1
Views: 2113
Reputation: 7392
There are many XML parsing libraries, each has its own strengths and annoyances, so you're going to find a broad range of solutions.
However, here is a nice snipplet that is small, fast and functions regardless of what libraries are installed because it's a regex.
You feed it an XML string and it returns an array of the whole dynamic structure:
$xml = file_get_contents('example.xml');
$data = xml2array($xml);
echo "<pre>" . print_r($data, true ) . "</pre>";
The function:
function xml2array($xml) {
$xmlary = array();
$reels = '/<(\w+)\s*([^\/>]*)\s*(?:\/>|>(.*)<\/\s*\\1\s*>)/s';
$reattrs = '/(\w+)=(?:"|\')([^"\']*)(:?"|\')/';
preg_match_all($reels, $xml, $elements);
foreach ($elements[1] as $ie => $xx) {
$xmlary[$ie]["name"] = $elements[1][$ie];
if ($attributes = trim($elements[2][$ie])) {
preg_match_all($reattrs, $attributes, $att);
foreach ($att[1] as $ia => $xx)
$xmlary[$ie]["attributes"][$att[1][$ia]] = $att[2][$ia];
}
$cdend = strpos($elements[3][$ie], "<");
if ($cdend > 0) {
$xmlary[$ie]["text"] = substr($elements[3][$ie], 0, $cdend - 1);
}
if (preg_match($reels, $elements[3][$ie]))
$xmlary[$ie]["elements"] = xml2array($elements[3][$ie]);
else if ($elements[3][$ie]) {
$xmlary[$ie]["text"] = $elements[3][$ie];
}
}
return $xmlary;
}
Upvotes: 1
Reputation: 10219
You can use SimpleXML.
Load your data with simplexml_load_string And then you can make a loop to parse the xml into an array thanks to the SimpleXML class
As it is called, it's pretty simple to do.
Upvotes: 5