php用正则读取不规范的xml

aries 发表于 2014-09-11 1598 次浏览

如果在你的程序中收到这样的字符串:

<ReportList><ordIndex>1</ordIndex><ordLabNo>1942268</ordLabNo><arcItemId>134</arcItemId><ordItemDesc>产品1</ordItemDesc><Status>执行</Status><ordDate>2013-08-12</ordDate><reportStatus>报告已出</reportStatus><reportException>0</reportException></ReportList><ReportList><ordIndex>2</ordIndex><ordLabNo>19434368</ordLabNo><arcItemId>135</arcItemId><ordItemDesc>产品2</ordItemDesc><Status>执行</Status><ordDate>2013-05-12</ordDate><reportStatus>报告未出</reportStatus><reportException>0</reportException></ReportList>

那么,恭喜你,php中我们常用的几种方法都不会生效,如:

$array = (array)new SimpleXMLElement($xml_str); 
$array = (array)simplexml_load_string($xml_str);
$array = json_decode(json_encode(simplexml_load_string($xml_str)),true);

都是返回 false

所以我们只能自己写个方法喽



代码如下:
function parse_xml_to_array($xmlstr,$loopTag){
    $args = explode('</'.$loopTag.'>',$xmlstr);
    $returns = array();
    if($args){
        $reg = '/<(w+)[^>]*>([x00-xFF]*)</1>/';
        foreach($args as $item){
            $item = str_replace('<'.$loopTag.'>','',$item);
            if(preg_match_all($reg, $item, $matches)) {
               if(isset($matches[1]) && isset($matches[2])){
                   $returns[] = array_combine($matches[1],$matches[2]);
               }
            }
        }
    }
    unset($args);
    return $returns;
}
$arr = parse_xml_to_array($xml,'ReportList');
var_dump($arr);

0条评论

如需评论,请填写表单。
换一个

记住我的信息