2011-03-04 174 views
0
<%@ 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。java jdom編輯xml文件

+0

什麼是你想怎麼辦? – Chris 2011-03-07 14:38:00

+0

我正在嘗試使用jdom編輯現有的xml文件,然後保存它。我允許用戶使用HTML組件(如文本框等)編輯它。請幫助我這個謝謝。 – Maverick 2011-03-13 13:14:31

回答

2

使用FileWriter.close()可以幫助關閉FileWriter的...

FileWriter fileWriter = new FileWriter("E:/workbench j2ee/cPEP_UI/WebContent/config.xml"); 
xmlOutputter.output(doc, fileWriter); 
fileWriter.close();