关于PHP获取XML的问题

2025-05-18 02:07:40
推荐回答(3个)
回答1:

关于你这个问题,我还是建议你用xml读取
这个是不会出错的
当然也可以用正则,不过如果items之间的属性位置变化了,我写的这个正则就不好用了,以下是保证属性位置不变的情况的例子,你把想得到的值,用foreach 循环$matches就可以
$xmlstr = @file_get_contents('data_show.xml');
$re='/$re_total='/@header("Content-Type: text/html; charset=utf-8");
if(preg_match($re_total, $xmlstr, $mat)){
print_r($mat);
echo '


';
}
if(preg_match_all($re, $xmlstr, $matches)){
print_r($matches);
echo '
';
}

回答2:

看手册

http://php.net/manual/en/book.xml.php

回答3:

下边是用dom函数处理的文件,正则处理的写到你另一个帖子里了

关于控制数据,你在循环里边写个判断就可以 if($i>100)break;
$dom = new DOMDocument();
$dom->load('8888.xml');
$totalnode = $dom->getElementsByTagName('variable_total')->item(0);
echo $totalnode ->getAttribute('totalman').' ';
echo $totalnode ->getAttribute('totalmenoy').' ';
echo $totalnode ->getAttribute('totaljifen').'


';

$allnodes = $dom->getElementsByTagName('variable_sysnew');
$i=0;
foreach ($allnodes as $node) {
if($i>100)break;
echo ' '.$node ->getAttribute('name').' ';
echo $node ->getAttribute('value').' ';
echo $node ->getAttribute('web_html').' ';
echo $node ->getAttribute('title_name').'
';
$i++;
}

exit;