import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.XMLWriter;
import java.io.*;public class XmlDom4J{
public void generateDocument(){
Document document = DocumentHelper.createDocument();
     Element catalogElement = document.addElement("catalog");
     catalogElement.addComment("An XML Catalog");
     catalogElement.addProcessingInstruction("target","text");
     Element journalElement =  catalogElement.addElement("journal");
     journalElement.addAttribute("title", "XML Zone");
     journalElement.addAttribute("publisher", "IBM developerWorks");
     Element articleElement=journalElement.addElement("article");
     articleElement.addAttribute("level", "Intermediate");
     articleElement.addAttribute("date", "December-2001");
     Element  titleElement=articleElement.addElement("title");
     titleElement.setText("Java configuration with XML Schema");
     Element authorElement=articleElement.addElement("author");
     Element  firstNameElement=authorElement.addElement("firstname");
     firstNameElement.setText("Marcello");
     Element lastNameElement=authorElement.addElement("lastname");
     lastNameElement.setText("Vitaletti");
     document.addDocType("catalog",
                           null,"file://c:/Dtds/catalog.dtd");
    try{
    XMLWriter output = new XMLWriter(
            new FileWriter( new File("e:/catalog.xml") ));
        output.write( document );
        output.close();
        }
     catch(IOException e){System.out.println(e.getMessage());}
}
public static void main(String[] argv){
XmlDom4J dom4j=new XmlDom4J();
dom4j.generateDocument();
}
}
生成内容<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE catalog SYSTEM "file://c:/Dtds/catalog.dtd"><catalog><!--An XML Catalog--><?target text?><journal title="XML Zone" publisher="IBM developerWorks"><article level="Intermediate" date="December-2001"><title>Java configuration with XML Schema</title><author><firstname>Marcello</firstname><lastname>Vitaletti</lastname></author></article></journal></catalog>这个代码生成的xml内容是对,但接下来如何控制其输出格式呢?
我想要标准的格式输出,是控制其dtd?<?xml version="1.0" encoding="ISO-8859-1"?><bookstore><book category="COOKING">
  <title lang="en">Everyday Italian</title>
  <author>Giada De Laurentiis</author>
  <year>2005</year>
  <price>30.00</price>
</book><book category="CHILDREN">
  <title lang="en">Harry Potter</title>
  <author>J K. Rowling</author>
  <year>2005</year>
  <price>29.99</price>
</book><book category="WEB">
  <title lang="en">XQuery Kick Start</title>
  <author>James McGovern</author>
  <author>Per Bothner</author>
  <author>Kurt Cagle</author>
  <author>James Linn</author>
  <author>Vaidyanathan Nagarajan</author>
  <year>2003</year>
  <price>49.99</price>
</book><book category="WEB">
  <title lang="en">Learning XML</title>
  <author>Erik T. Ray</author>
  <year>2003</year>
  <price>39.95</price>
</book></bookstore>
有空格及换行的

解决方案 »

  1.   

    这东西就格式化
    所以搜索  dom4j 格式化  就有很多答案OutputFormat format = OutputFormat.createPrettyPrint();        
            //制定输出xml的编码类型   
            format.setEncoding("gb2312");   
               
            StringWriter writer = new StringWriter();   
            //创建一个文件输出流   
            XMLWriter xmlwriter = new XMLWriter( writer, format );   
            //将格式化后的xml串写入到文件   
            xmlwriter.write(document);