package gtjahf.xml;
import java.io.*; 
 import org.jdom.*; 
 import org.jdom.input.*; 
 import org.jdom.output.*; public class CreateXml{

public CreateXml(){}public void addmessage(String xmlDir_rootid, String strID, String strTitle, String strTime,String clickTimes,String strContent) {
try 
   { 
    Document doc = new Document();    
    ProcessingInstruction pi=new ProcessingInstruction("xml-stylesheet","type='text/xsl' href='showpage.xsl'"); 
    doc.addContent(pi); 
    
     //  Namespace ns = Namespace.getNamespace("http://www.bromon.org" ); 
      Element root = new Element("SHOWPAGE"); 
    //root.addNamespaceDeclaration(ns2); 
    doc.setRootElement(root); 
    
    Element newID = new Element("ID"); 
    //el1.setAttribute("属性", "属性一"); 
    Text strid=new Text(strID); 
    newID.addContent(strid); 
    
    Element newTitle = new Element("TITLE").addContent(strTitle);               
    Element newDate = new Element("DATE").addContent(strTime);               
    Element newClickTimes = new Element("CLICKTIMES").addContent(clickTimes);               
    Element newContent = new Element("CONTENT").addContent( strContent);               
                
             root.addContent(newID); 
             root.addContent(newTitle); 
             root.addContent(newDate); 
             root.addContent(newClickTimes); 
             root.addContent(newContent); 
             
             //缩进四个空格,自动换行,gb2312编码 
             //XMLOutputter outputter = new XMLOutputter("  ", true,"GB2312"); 
             Format format = Format.getCompactFormat();
     format.setEncoding("gb2312"); //设置xml文件的字符为gb2312ss
     format.setIndent("    ");
     XMLOutputter XMLOut = new XMLOutputter(format);
             XMLOut.output(doc, new FileWriter("d:\\Tomcat\\webapps\\web\\xmlpages\\"+xmlDir_rootid+"\\"+strID+".xml")); 
         }catch(Exception e) 
         { 
          System.out.println(e); 
         } 
你看看吧