【问题标题】:How to change XML attribute on the client side and then save the result on the server side?如何在客户端更改 XML 属性,然后在服务器端保存结果?
【发布时间】:2011-03-28 12:54:27
【问题描述】:

我的问题是:

我从服务器端读取了一个 xml 文件,然后将它们呈现给客户端,接下来我想编辑数据,例如:使用 setAttribute() 方法来更改它们。现在问题来了: 我不想只在客户端修改它们,但也在服务器端,它们保存 xml 文件。我怎么能做到这一点,使用 JSP 和 Javascript ? 这是一些初步的想法,但有些部分不起作用...... 如行:“ = length;”

而且我认为如果我逐行编写 xml 确实会使页面加载缓慢... 有没有更好的办法?

谢谢你:)

<% String attribute[];
   int count;  %>
<script>
  //hide the part of reading xml file to xmlDoc
  var length = xmlDoc.getElementsByTagName("item").length;
  <%count%> = length; // this doesnt work ...???
  for(int i = 0; i < length; i++)
 {
    xmlDoc.getElementsByTagName("item").item[i].setAttribute("score","1");
    <%attribute[i]%> =  xmlDoc.getElementsByTagName("item").item[i].setAttribute("score");   
}
</script>
<%   String xmlString;
     String personNm ={"Bob","Mike","Lily"};
        for (int i;i < count;i++)
           xmlString = "<person score="+attribute[i]+">personNm[i]</person>";

      //here i use a out put buffer to print it line by line...
     outputFile = new File("result.xml");
     outputFile.createNewFile();
     FileWriter outfile = new FileWriter(outputFile);
     outfile.write(xmlString);
     outfile.close();  %>

【问题讨论】:

  • 如果你想让某人感兴趣,你需要在提问时下功夫!
  • @enguerran 做了什么努力?...添加更多标签?还是?
  • 注意演示。英语语言和代码格式。
  • 正如 BalusC 所说:注意演示。
  • 是的,谢谢,你说得对,我又修改了一遍,希望现在更清楚,但我不是来自英语国家...所以我尽力了:)

标签: javascript xml ajax jsp xml-parsing


【解决方案1】:

你必须明白JSP是在服务器上执行的,而Javascript是在客户端上执行的。在服务器上只执行 JSP 并将结果发送到服务器。您的客户将收到以下内容:

<script>
//read xml file to xmlDoc

var length = xmlDoc.getElementsByTagName("item").length;
 = length;
for(int i = 0; i < length; i++)
{
   xmlDoc.getElementsByTagName("item").item[i].setAttribute("score","1");
    = xmlDoc.getElementsByTagName("item").item[i].setAttribute("score");
}
</script>

请注意,所有在服务器上执行的 JSP 标记以及浏览器接收到的都是结果。 服务器上不执行任何 Javascript

您可能想要做的是在服务器上修改您的 XML,然后在页面中返回结果。

【讨论】:

  • 所以我无法将客户端的值转移到服务器端?
  • @Camellia 有可能,只是您尝试这样做的方式并非如此。您必须使用AJAX 与服务器通信。
  • 谢谢,但是AJAX现在对我来说是个大课题,我没有足够的时间从头一步步研究它,你能帮我指出我可以使用哪种功能吗?
  • @Camellia Ajax 与 jQuery 一起使用相当简单。如果需要示例,可以查看 jQuery 的文档:api.jquery.com/jQuery.get
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-08-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多