简体   繁体   中英

How to dynamically extract data from XML in PHP?

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.

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.

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;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM