如何更改与jQuery的XML节点的值? [英] How to change the value of an xml node with jquery?

查看:183
本文介绍了如何更改与jQuery的XML节点的值?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这可能是一个简单的问题,但不容弄清楚如何做到这一点。 我想加载和修改XML文件,然后通过PHP保存XML。

下面是code:

  $。阿贾克斯({
  键入:GET,
  网址:menu.xml文件,
  数据类型:XML,
  成功:函数(XML){
   $(XM​​L).find('MENU_ITEM')。每个(函数(){
    //改变MENU_ITEM的价值
    $(本).empty();
    $(本)的.text($(文本域)ATTR(价值));
    //发送XML到PHP
    .post的$('save_xml.php',$(XML),功能(数据){警报(数据加载:+数据);});
   }

  }
 });
 

下面是如何save_xml.php是这样的:

 < PHP

    $ XML = $ GLOBALS [HTTP_RAW_POST_DATA];
    $文件=的fopen(file.xml,W);
    FWRITE($文件,$ XML);
    fclose函数($文件);
    回声确定;

?>
 

解决方案

这是你正在寻找什么?

$(本)是每个 menu_items 你迭代与。每个的()

您code变为

  $(XML).find('MENU_ITEM')。每个(函数(){
   $(本)的.text(新价值);
});
 

希望这有助于

修改

要张贴此回服务器,我会做这样的:

  $后期('save_xml.php',{XML:$(XML)},功能(数据){警报(数据加载:+数据);});
 

然后在PHP文件

 < PHP
  $ XML = $ _ POST ['XML'];
  $文件=的fopen(file.xml,W);
  FWRITE($文件,$ XML);
  fclose函数($文件);
  回声确定;
?>
 

这code是未经检验的,而且有可能是任何数量的原因,这是行不通的,写在文件的权限等。

This may be a simple question though can´t figure out how to do it. I want to load and modify an xml file, then save the xml through php.

Here is the code:

 $.ajax({
  type: "GET",
  url: "menu.xml",
  dataType: "xml",
  success: function(xml) {
   $(xml).find('menu_item').each(function(){
    //change the value of menu_item
    $(this).empty();
    $(this).text($("textarea").attr("value"));
    //send xml to php
    $.post('save_xml.php', $(xml), function(data){alert("Data Loaded: " + data);});
   }

  }
 });

Here is how save_xml.php looks like:

<?php

    $xml = $GLOBALS["HTTP_RAW_POST_DATA"];
    $file = fopen("file.xml","w");
    fwrite($file, $xml);
    fclose($file);
    echo "ok";

?> 

解决方案

Is this what you are looking for?

$(this) is each of the menu_items you iterate over with .each()

Your code becomes

$(xml).find('menu_item').each(function(){
   $(this).text("New Value");
});

Hope this helps

EDIT

To post this back to the server I would do this:

$.post('save_xml.php', { xml: $(xml)}, function(data){alert("Data Loaded: " + data);});

and then in the PHP file

<?php
  $xml = $_POST['xml'];
  $file = fopen("file.xml","w");
  fwrite($file, $xml);
  fclose($file);
  echo "ok";
?> 

This code is untested, and there could be any number of reasons it doesn't work, write permissions on the files etc.

这篇关于如何更改与jQuery的XML节点的值?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆