如何在客户端更改XML属性,然后将结果保存在服务器端?

发布于 2024-10-27 17:43:26 字数 1233 浏览 1 评论 0原文

我的问题是:

我从服务器端读取一个 xml 文件,然后将它们呈现到客户端,接下来我想编辑数据,例如:使用 setAttribute() 方法来更改它们。现在问题来了: 我不想只在客户端修改它们,还要在服务器端修改它们,并且它们保存 xml 文件。我怎样才能使用 JSP 和 Javascript 做到这一点? 这是一些初步的想法,但有些部分不起作用...... 比如这行:“ <%count%> = 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();  %>

My Question is :

I read a xml file from server side and then present them into client side, next I want to edit the data, like : using setAttribute() method to change them. now here comes problem:
I dont want to only modified them on the client side, but on the server side also, and them save the xml file. how can I do that, using JSP and Javascript ?
Here is some initial ideas, but some part is not working ...
such as the line : " <%count%> = length; "

and i think it is really make the page load slowly if i write xml line by line...
is there a better to do this ?

thank you :)

<% 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();  %>

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

玩物 2024-11-03 17:43:26

你必须明白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,然后在页面中返回结果。

You have to understand that JSP is executed on the server and Javascript will be executed on the client. On the server only the JSP is executed and the result is sent to the server. What your client will receive is the following :

<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>

Notice that all the JSP tag where executed on the server and what the browser receive is the result. No Javascript is executed on the server.

What you probably want to do is to modify your XML on the server and after that return the result in the page.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文