duancuo1234 2010-11-23 11:02
浏览 60
已采纳

将发布的值保存到php中的xml

<root>
  <gallery name="First"/>
  <gallery name="Second"/>
  <gallery name="Third"/>
</root>

I think I have solved part of a previous problem and now have the foreach loop correct? Still I can't get it to save the xml.

$objXML = new SimpleXMLElement(XML_FILE_NAME, null, true);  
foreach($objXML->xpath('/root/gallery/@name') as $key => $old){
    $new = $_POST['name'][$key];
    echo "$key : $old : $new<br />
";
    $old = $new; // this does nothing??
    }
$objXML->asXML(XML_FILE_NAME);

echo returns:
0 : First : First New
1 : Second : Second New
2 : Third : Third New

Why does this not save the new posted values for 'name' back to my XML doc, what am I doing wrong?

  • 写回答

1条回答 默认 最新

  • doufu2496 2010-11-23 11:09
    关注

    You're in a loop.

    This mean each time the loop comes back to another point $key and $old get their respective new values.

    I guess that's what you're trying to achieve (but I'm not you're sure I got your goal here)

    foreach($objXML->xpath('/root/gallery/@name') as $key => $value){
        $new = $_POST['name'][$key];
        echo "$key : $old : $new<br />
    ";
        $old = $value;
    }
    

    From your comment :

    You can test it here.

    $objXML = new SimpleXMLElement(XML_FILE_NAME, null, true);  
    
    $galleryLore = $objXML->gallery; // finding gallery
    $i = 0;
    foreach($galleryLore as $gallery){
      unset($gallery['name']); // We delete the old name
      $new = $_POST['name'][$i]; // We find the new name value
      $gallery->addAttribute('name', $new); // We add the new attribute we deleted before
      $i++;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 抖音咸鱼付款链接转码支付宝
  • ¥15 ubuntu22.04上安装ursim-3.15.8.106339遇到的问题
  • ¥15 求螺旋焊缝的图像处理
  • ¥15 blast算法(相关搜索:数据库)
  • ¥15 请问有人会紧聚焦相关的matlab知识嘛?
  • ¥15 网络通信安全解决方案
  • ¥50 yalmip+Gurobi
  • ¥20 win10修改放大文本以及缩放与布局后蓝屏无法正常进入桌面
  • ¥15 itunes恢复数据最后一步发生错误
  • ¥15 关于#windows#的问题:2024年5月15日的win11更新后资源管理器没有地址栏了顶部的地址栏和文件搜索都消失了