Home >Backend Development >PHP Tutorial >新手发个php修改XML的问题

新手发个php修改XML的问题

WBOY
WBOYOriginal
2016-06-23 14:06:191025browse

$dom=new DOMDocument('1.0');$dom->load('ceshi.xml');$root=$dom->getElementsByTagName('online');$root=$root->item(0);$userid=$root->getElementsByTagName('userid');foreach($userid as $b){  foreach($b->attributes as $attr){ $value=$attr->nodeValue;$name[].=$attr->nodeName;{if($value=="xiao"){     print_r($name);   } }}}$dom->save('ceshi.xml');
 
 
来个xml
<?xml version="1.0"?><online> <userid time="2007-06-13 11:20:08" ip="wangqile" label="wang">xiaxin</userid> <userid time="2007-06-13 13:22:15" pp="dasdsa" label="xiao" ip="dasdsa">rwet</userid> <userid time="2007-06-13 17:17:00" ip="dasdsa" label="ji"> gaoji</userid> </online>


问题是按上面那种写法只循环label="xiao"的那一行,也就是只返回那一行nodename值。。
要是按上面写的话返回的是第一行第二行一起返回的nodename值。。我也没想出来怎么改~~新手求教啊!!!


回复讨论(解决方案)

你提前打印了$name 数组当然是那个样子啦,你在外层foreach下面打印$name 就对了。

你提前打印了$name 数组当然是那个样子啦,你在外层foreach下面打印$name 就对了。

试过了 不是这个原因~~你可以看看啊  打印出的是前两行的值。。哈哈

这个意思?

$dom=new DOMDocument('1.0');$dom->load('ceshi.xml');$root=$dom->getElementsByTagName('online');$root=$root->item(0);$userid=$root->getElementsByTagName('userid');foreach($userid as $b) {  if($b->attributes->getNamedItem('label')->nodeValue == 'xiao') {    foreach($b->attributes as $attr){      $res[$attr->nodeName] = $attr->nodeValue;    }    print_r($res);  }}
Array
(
    [time] => 2007-06-13 13:22:15
    [pp] => dasdsa
    [label] => xiao
    [ip] => dasdsa
)

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn