java jdom编辑xml文件
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import= "org.jdom.*, java.util.*,
org.jdom.input.SAXBuilder,org.jdom.output.XMLOutputter,java.io.*" %>
<%
SAXBuilder saxBuilder = new SAXBuilder();
Document doc = saxBuilder.build("http://localhost:8080/cPEP_UI/config.xml");
%>
<html>
<head><title>Configuration Manager</title></head>
<body>
<h1><font color='green'>Configuration Manager</font></h1>
<%! private String type;
Element childNode;
Element create;
Element parent;
%>
<%
List list = doc.getRootElement().getChildren();
Iterator iter = list.iterator();
while (iter.hasNext()){
Element element = (Element) iter.next();
List NameDOBCity = element.getChildren();
Iterator listIter = NameDOBCity.iterator();
%>
<%
while ( listIter.hasNext() ){
childNode = (Element) listIter.next();
String param = request.getParameter(childNode.getName()+childNode.getAttributeValue("id"));
Enumeration e = request.getParameterNames();
while(e.hasMoreElements()){
String s = (String)e.nextElement();
if(s.equalsIgnoreCase(childNode.getName()))
childNode.setText(param);
}
%>
<%
}
}
XMLOutputter xmlOutputter = new XMLOutputter();
xmlOutputter.output(doc, new FileWriter("E:/workbench j2ee/cPEP_UI/WebContent/config.xml"));
%>
</body>
</html>
** 代码有什么问题?!它没有显示任何错误,但也没有更改元素文本。实际上我正在制作一个 gui,需要使用 jsp 中的文本框编辑 config.xml。
<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
pageEncoding="ISO-8859-1"%>
<%@ page import= "org.jdom.*, java.util.*,
org.jdom.input.SAXBuilder,org.jdom.output.XMLOutputter,java.io.*" %>
<%
SAXBuilder saxBuilder = new SAXBuilder();
Document doc = saxBuilder.build("http://localhost:8080/cPEP_UI/config.xml");
%>
<html>
<head><title>Configuration Manager</title></head>
<body>
<h1><font color='green'>Configuration Manager</font></h1>
<%! private String type;
Element childNode;
Element create;
Element parent;
%>
<%
List list = doc.getRootElement().getChildren();
Iterator iter = list.iterator();
while (iter.hasNext()){
Element element = (Element) iter.next();
List NameDOBCity = element.getChildren();
Iterator listIter = NameDOBCity.iterator();
%>
<%
while ( listIter.hasNext() ){
childNode = (Element) listIter.next();
String param = request.getParameter(childNode.getName()+childNode.getAttributeValue("id"));
Enumeration e = request.getParameterNames();
while(e.hasMoreElements()){
String s = (String)e.nextElement();
if(s.equalsIgnoreCase(childNode.getName()))
childNode.setText(param);
}
%>
<%
}
}
XMLOutputter xmlOutputter = new XMLOutputter();
xmlOutputter.output(doc, new FileWriter("E:/workbench j2ee/cPEP_UI/WebContent/config.xml"));
%>
</body>
</html>
** whats wrong with the code?! Its not showing any error, but not changing the element text either. Actually I am making an gui, and need to edit config.xml using textbox in jsp.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
使用 FileWriter.close() 关闭 Filewriter 可能会有所帮助...
Closing the Filewriter using
FileWriter.close()
could help...