Home  >  Article  >  Backend Development  >  新手发个php修改XML的有关问题!

新手发个php修改XML的有关问题!

WBOY
WBOYOriginal
2016-06-13 12:48:25849browse

新手发个php修改XML的问题!!

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


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


------解决方案--------------------
本帖最后由 xuzuning 于 2013-04-20 10:25:31 编辑 这个意思?
$dom=new DOMDocument('1.0');<br />
$dom->load('ceshi.xml');<br />
$root=$dom->getElementsByTagName('online');<br />
$root=$root->item(0);<br />
$userid=$root->getElementsByTagName('userid');<br />
foreach($userid as $b) {<br />
  if($b->attributes->getNamedItem('label')->nodeValue == 'xiao') {<br />
    foreach($b->attributes as $attr){<br />
      $res[$attr->nodeName] = $attr->nodeValue;<br />
    }<br />
    print_r($res);<br />
  }<br />
}<br />
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