import org.jdom.*;
import org.jdom.input.*;
import org.jdom.output.*;XMLOutputter outputter = new XMLOutputter("  ", true,"gb2312");
outputter.output(servDoc, filename);

解决方案 »

  1.   

    不想用jdom的说
    用jdk1.4呢?
      

  2.   

    给你一个例子,供参考:
    javax.xml.transform.TransformerFactory ff=javax.xml.transform.TransformerFactory.newInstance();
    javax.xml.transform.Transformer a=ff.newTransformer(); 
    java.io.File f=new java.io.File("c:\\study\\dom.txt");
    DocumentBuilderFactory df=DocumentBuilderFactory.newInstance();
    DocumentBuilder db=df.newDocumentBuilder();
    Document d=db.newDocument();
    Element e=d.createElement("test"); 
      Result r=new StreamResult(f);
    Source s=new DOMSource(e);
    a.transform( s, r);
      

  3.   

    XMLDocument package at jdk1.4 inner,you have at org.*.*
    or javax.xml.*... find it((XMLDocument)doc).write( your stream or file)
      

  4.   

    jdk1.4里没有找到XMLDocument啊!
      

  5.   

    http://java.sun.com/xml/downloads/javaxmlpack.html
    问题应该解决了。
      

  6.   

    一个完整的例子,JB7+JDK1.4调试通过.JAVA原文件:
    --------------
    package com.holen;import java.io.*;
    import javax.xml.parsers.*;
    import javax.xml.transform.*;
    import javax.xml.transform.dom.*;
    import javax.xml.transform.stream.*;
    import org.w3c.dom.*;
    import org.xml.sax.*;public class CreateXMLFile {  public CreateXMLFile() {
      }  public void CreateXMLFile(String inFile) throws Exception {
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.newDocument();
        Element root = doc.createElement("content");
        doc.appendChild(root);    Element title = doc.createElement("title");
        root.appendChild(title);
        Text ttitle = doc.createTextNode("用XSLT进行断行");
        title.appendChild(ttitle);    Element author = doc.createElement("author");
        root.appendChild(author);
        Text tauthor = doc.createTextNode("Kevin Manley");
        author.appendChild(tauthor);    Element body = doc.createElement("body");
        root.appendChild(body);
        String str_body ="this is the body";
        Text tbody = doc.createTextNode(str_body);
        body.appendChild(tbody);    try{
          TransformerFactory tff = TransformerFactory.newInstance();
          Transformer tf = tff.newTransformer();
          DOMSource source = new DOMSource(doc);
          StreamResult rs = new StreamResult(new File(inFile));
          tf.transform(source,rs);
        }catch(Exception e){
          e.printStackTrace();
        }
      }  public static void main(String[] args) throws Exception {
        CreateXMLFile createXMLFile1 = new CreateXMLFile();
        createXMLFile1.CreateXMLFile("ww.xml");
      }
    }---------
    生成的XML文件:
    ---------
      <?xml version="1.0" encoding="UTF-8" ?> 
      <content>
      <title>用XSLT进行断行</title> 
      <author>Kevin Manley</author> 
      <body>this is the body</body> 
      </content>