Dom4j寫XML文件

package upload.dom4j.xml;

import java.io.File;
import java.io.IOException;
import java.io.PrintWriter;

import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;



public class ParseXML {

	public static void main(String[] args) throws Exception{
		ParseXML dom4j=new ParseXML();
		dom4j.createXML();

	}
	public void createXML()throws Exception{
		Document document = DocumentHelper.createDocument();
		document.addDocType("NmLoader", null,"standardX05.dtd");
		document.setXMLEncoding("UTF-8");
		System.out.println("Encoding = " + document.getXMLEncoding());
		Element nmLoader = document.addElement("NmLoader");
		String[] docName = {"doc001","doc002"};
		String[] docTitle = {"doc001title","doc002title"};
		String[] docSaveIn = {"/Default/SIP","/Default/SIP"};
		String[] docTypeDef = {"com.efoxocnn.mseg.FAI_Report","com.efoxocnn.mseg.DesignReview"};
		String[] docPath = {"中國奧運年.doc","北京奧運.doc"};
		for(int i=0;i<docName.length;i++) {
			Element beginElement =  nmLoader.addElement("csvBeginWTDocument");
			beginElement.addAttribute("handler", "wt.doc.LoadDoc.beginCreateWTDocument");
			Element nameElement =  beginElement.addElement("csvname");
			nameElement.setText(docName[i]);
			Element titleElement =  beginElement.addElement("csvtitle");
			titleElement.setText(docTitle[i]);
			Element numberElement =  beginElement.addElement("csvnumber");
			Element typeElement =  beginElement.addElement("csvtype");
			typeElement.setText("Document");
			Element descElement =  beginElement.addElement("csvdescription");
			Element departElement =  beginElement.addElement("csvdepartment");
			departElement.setText("DESIGN");
			Element saveInElement =  beginElement.addElement("csvsaveIn");
			saveInElement.setText(docSaveIn[i]);
			Element teamElement =  beginElement.addElement("csvteamTemplate");
			Element domainElement =  beginElement.addElement("csvdomain");
			Element lifecycleElement =  beginElement.addElement("csvlifecycletemplate");
			Element stateElement =  beginElement.addElement("csvlifecyclestate");
			Element typedefElement =  beginElement.addElement("csvtypedef");
			typedefElement.setText(docTypeDef[i]);
			Element versionElement =  beginElement.addElement("csvversion");
			Element iterationElement =  beginElement.addElement("csviteration");			
			Element endElement =  nmLoader.addElement("csvEndWTDocument");
			endElement.addAttribute("handler", "wt.doc.LoadDoc.endCreateWTDocument");
			Element contenttypeElement =  endElement.addElement("csvprimarycontenttype");
			contenttypeElement.setText("ApplicationData");
			Element pathElement =  endElement.addElement("csvpath");
			pathElement.setText(docPath[i]);
			Element formatElement =  endElement.addElement("csvformat");
			Element contdescElement =  endElement.addElement("csvcontdesc");
			Element parentContainerPathElement =  endElement.addElement("csvparentContainerPath");
		}
	    String encoding = System.getProperty("file.encoding");
		System.out.println(encoding);
	    try{
	    	   OutputFormat format = OutputFormat.createPrettyPrint();	    	   
	    	   format.setEncoding("UTF-8");
	    	   format.setOmitEncoding(false);
	    	          
	        XMLWriter output = new XMLWriter(
	                new PrintWriter( new File("c:/tmp/conf/importDoc.xml"),"UTF-8"),format);
            output.write( document );
            output.close();
	    } catch(IOException e){
	    	System.out.println(e.getMessage());
	    }

	}
}

 

你可能感兴趣的:(C++,c,xml,C#)