2010-10-15 12:33
浏览 81


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.

图片转代码服务由CSDN问答提供 功能建议

我有一个字符串变量,包含XML数据,我想知道是否有一种简单的方法来提取字段和数据 从文件。 通过迭代泛型方式,无需在代码中指定标记的名称。

  • 写回答
  • 好问题 提建议
  • 关注问题
  • 收藏
  • 邀请回答

2条回答 默认 最新

  • donqo88682 2010-10-15 12:37

    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.

    解决 无用
    打赏 举报
  • dougong7850 2010-10-15 12:54

    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;
    解决 无用
    打赏 举报

相关推荐 更多相似问题