JDOM生成XML文档(1)

package com.ninemax.common.utils;

import java.io.FileOutputStream;

import org.jdom.DocType;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;

/**
 * JAVA生成XML
 * 
 * @author Darker
 *
 */
public class JavaTransXml {

	public static void JavaTransXml2() throws Exception {
		//生成XML文档对象
		Document doc= new Document();
		//生成XM文档根节点
		Element root= new Element("ArticleSet");
		//文档头声明
		doc.addContent(new DocType("ArticleSet","WPRIM_2.dtd"));
		//根节点添加到文档中
		doc.addContent(root);
		//生成10个子节点
		for(int i=0;i<10;i++){
			Element article= new Element("Article");
			Element journal= new Element("Journal");
			journal.addContent(new Element("Country").setText("China"));
			journal.addContent(new Element("PublisherName").setText("IMICAMS"));
			journal.addContent(new Element("JournalTitle").setText("first testing JournalTitle"));
			journal.addContent(new Element("Issn").setText("0000-0001"));
			journal.addContent(new Element("Volume").setText("2"));
			journal.addContent(new Element("Issue").setText("2"));
			Element pubDate=new Element("PubDate");
			pubDate.addContent(new Element("Year").setText("2009"));
			pubDate.addContent(new Element("Month").setText("05"));
			pubDate.addContent(new Element("Day").setText("18"));
			journal.addContent(pubDate);
			article.addContent(journal);
			article.addContent(new Element("ArticleTitle").setText("first testing ArticleTitle"));
			article.addContent(new Element("VernacularTitle").setText("first testing VernacularTitle"));
			article.addContent(new Element("FirstPage").setText("1"));
			article.addContent(new Element("LastPage").setText("10"));
			article.addContent(new Element("Language").setText("Chinese"));
			Element authorList=new Element("AuthorList");
			for(int j=0;j<2;j++){
				
				Element author=new Element("Author");
				author.addContent(new Element("FirstName").setText("FirstName1"));
				author.addContent(new Element("MiddleName1").setText("MiddleName1"));
				author.addContent(new Element("LastName").setText("LastName1"));
				author.addContent(new Element("Affiliation").setText("Affiliation1"));
				authorList.addContent(author);
			}
			Element author=new Element("Author");
			author.addContent(new Element("CollectiveName").setText("Plastic Surgery Educational Foundation DATA Committee"));
			authorList.addContent(author);
			article.addContent(new Element("PublicationType").setText("Journal Article"));
			Element articleIdList=new Element("ArticleIdList");
			articleIdList.addContent(new Element("ArticleId").setText("10.1055/s-2007-985171").setAttribute("IdType","doi"));
			articleIdList.addContent(new Element("ArticleId").setText("http://www.imicams.ac.cn").setAttribute("IdType","url"));
			article.addContent(articleIdList);
			article.addContent(new Element("Abstract").setText("...."));
			Element keywordsList=new Element("KeywordsList");
			
			for(int k=0;k<2;k++){
				
				keywordsList.addContent(new Element("Keywords").setText("Keywords1"));
				
			}
			article.addContent(keywordsList);
			
			Element meSHList=new Element("MeSHList");
			
			for(int h=0;h<2;h++){
				
				meSHList.addContent(new Element("MeSH").setText("MeSH1"));
				
			}
			article.addContent(meSHList);
			//给父节点ArticleSet添加子节点Article
			root.addContent(article);
		}
		XMLOutputter XMLOut = new XMLOutputter(Format.getPrettyFormat());  
        // 输出 XMLOutput.xml 文件到项目根目录;     
        XMLOut.output(doc, new FileOutputStream("E://XMLOutput.xml")); 
        // 参考网站:http://budani.iteye.com/blog/1630493
	}
	
	public static Format FormatXML(){  
        //格式化生成的xml文件,如果不进行格式化的话,生成的xml文件将会是很长的一行...
		//默认编码为:getRawFormat()
        Format format = Format.getPrettyFormat();
        format.setEncoding("utf-8");  
        format.setIndent(""); 
        return format;  
    }

	public static void main(String[] args) throws Exception {
		
		JavaTransXml.JavaTransXml2();
		
		System.out.println("生成 mxl 文件成功,请查看项目根目录XMLOutput.xml文件!");
	}


}

生成XML文档内容:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE ArticleSet SYSTEM "WPRIM_2.dtd">

<ArticleSet>
  <Article>
    <Journal>
      <Country>China</Country>
      <PublisherName>IMICAMS</PublisherName>
      <JournalTitle>first testing JournalTitle</JournalTitle>
      <Issn>0000-0001</Issn>
      <Volume>2</Volume>
      <Issue>2</Issue>
      <PubDate>
        <Year>2009</Year>
        <Month>05</Month>
        <Day>18</Day>
      </PubDate>
    </Journal>
    <ArticleTitle>first testing ArticleTitle</ArticleTitle>
    <VernacularTitle>first testing VernacularTitle</VernacularTitle>
    <FirstPage>1</FirstPage>
    <LastPage>10</LastPage>
    <Language>Chinese</Language>
    <PublicationType>Journal Article</PublicationType>
    <ArticleIdList>
      <ArticleId IdType="doi">10.1055/s-2007-985171</ArticleId>
      <ArticleId IdType="url">http://www.imicams.ac.cn</ArticleId>
    </ArticleIdList>
    <Abstract>....</Abstract>
    <KeywordsList>
      <Keywords>Keywords1</Keywords>
      <Keywords>Keywords1</Keywords>
    </KeywordsList>
    <MeSHList>
      <MeSH>MeSH1</MeSH>
      <MeSH>MeSH1</MeSH>
    </MeSHList>
  </Article>
 </ArticleSet>

XML文档太长,截了其中一部分..

参考文献:http://blog.csdn.net/ivory_lei/article/details/6892537

企鹅:61489385

你可能感兴趣的:(xml,jdom)