如何在客户端更改XML属性,然后将结果保存在服务器端?
我的问题是:
我从服务器端读取一个 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
你必须明白JSP是在服务器上执行的,而Javascript将在客户端上执行。在服务器上仅执行JSP,并将结果发送到服务器。您的客户端将收到以下内容:
请注意,所有在服务器上执行的 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 :
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.