新手发个php修改XML的问题

WBOY
Release: 2016-06-23 14:06:19
Original
898 people have browsed it

$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');
Copy after login


来个xml
 xiaxin rwet  gaoji 
Copy after login


问题是按上面那种写法只循环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);  }}
Copy after login
Array
(
    [time] => 2007-06-13 13:22:15
    [pp] => dasdsa
    [label] => xiao
    [ip] => dasdsa
)

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!