JDOM生成XML文档(二)

package com.ninemax.util;

import java.io.FileOutputStream;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

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

import com.jfinal.plugin.activerecord.Db;
import com.jfinal.plugin.activerecord.Record;

/**
 * 手动导出数据库数据(XML)
 * 
 * @author Darker
 * 
 */
public class ExportXmlUtil {

	public static void BulkExportXml() {
		
		SimpleDateFormat df=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss.SSS");
		
		Document doc = new Document();
		// 添加注释
		doc.addContent(new Comment("CreateTime:"+df.format(new Date())));
		
		Element root = new Element("ArticleSet");
		// 文档头声明
		doc.addContent(new DocType("ArticleSet", "WPRIM_2.dtd"));
		// 头节点添加到文档中
		doc.addContent(root);
		// 查询导出记录表
		Record exportLog = Db.findFirst("SELECT TOP 1 * FROM Wprim_Export_log ORDER BY id DESC ");
		// 查询article数据
		List<Record> articleList = Db.find("SELECT * FROM WSSARTICLE WHERE crtime>? ORDER BY crtime DESC",exportLog.getStr("export_time"));
		if(articleList!=null){
			// 添加节点
			for (Record articleRecord : articleList) {
				// Journal节点信息
				Record journalRecord = Db.findFirst("SELECT * FROM WSSJOURNAL WHERE JournalId=?",articleRecord.getInt("JID"));
				// Volume节点信息
				Record volumeRecord = Db.findFirst("SELECT * FROM WSSJOURNALVOLUME WHERE JID=?", articleRecord.getInt("JID"));
				Element article = new Element("Article");
				Element journal = new Element("Journal");
				journal.addContent(new Element("Country").setText(journalRecord.getStr("country")==""?"":journalRecord.getStr("country")));
				journal.addContent(new Element("PublisherName").setText(journalRecord.getStr("publisher")==""?"":journalRecord.getStr("publisher")));
				journal.addContent(new Element("JournalTitle").setText(journalRecord.getStr("journalTitle")==""?"":journalRecord.getStr("journalTitle")));
				journal.addContent(new Element("Issn").setText(journalRecord.getStr("ISSN") ==""?(journalRecord.getStr("EISSN") ==""?"":journalRecord.getStr("EISSN")) : journalRecord.getStr("ISSN")));
				journal.addContent(new Element("Volume").setText(volumeRecord.getStr("Volume")==""?"":journalRecord.getStr("volume")));
				journal.addContent(new Element("Issue").setText(volumeRecord.getStr("Issue")==""?"":journalRecord.getStr("IssuesNumber")));
				Element pubDate = new Element("PubDate");
				pubDate.addContent(new Element("Year").setText(String.valueOf(volumeRecord.getInt("PubYear"))==""?"":String.valueOf(volumeRecord.getInt("PubYear"))));
				pubDate.addContent(new Element("Month").setText(volumeRecord.getStr("PubMonth")==""?"":volumeRecord.getStr("PubMonth")));
				pubDate.addContent(new Element("Day").setText(volumeRecord.getStr("PubDay")==""?"":volumeRecord.getStr("PubDay")));
				journal.addContent(pubDate);
				article.addContent(journal);
				// article节点信息
				article.addContent(new Element("ArticleTitle").setText(articleRecord.getStr("ArticleTitle")==""?"":articleRecord.getStr("ArticleTitle")));
				article.addContent(new Element("VernacularTitle").setText(articleRecord.getStr("VernacularTitle")==""?"":articleRecord.getStr("VernacularTitle")));
				article.addContent(new Element("FirstPage").setText(articleRecord.getStr("FirstPage")==""?"":articleRecord.getStr("FirstPage")));
				article.addContent(new Element("LastPage").setText(articleRecord.getStr("LastPage")==""?"":articleRecord.getStr("LastPage")));
				article.addContent(new Element("Language").setText(articleRecord.getStr("Language")==""?"":articleRecord.getStr("Language")));
				Element authorList = new Element("AuthorList");
				// 特殊值Author处理
				String str = articleRecord.getStr("Author");
				
				if (str != null) {
					
					String[] authors = str.split(";");
					
					for (int j = 0; j < authors.length; j++) {
						Element author = new Element("Author");
						author.addContent(new Element("FirstName").setText((authors[j].split("-")[0]).split(" ")[0]==""?"":(authors[j].split("-")[0]).split(" ")[0]));
						author.addContent(new Element("MiddleName").setText((authors[j].split("-")[0]).split(" ")[1] ==""?"":(authors[j].split("-")[0]).split(" ")[1]));
						author.addContent(new Element("LastName").setText((authors[j].split("-")[0]).split(" ")[2] ==""?"":(authors[j].split("-")[0]).split(" ")[2]));
						author.addContent(new Element("Affiliation").setText(authors[j].split("-")[1] ==""?"":authors[j].split("-")[1]));
						authorList.addContent(author);
					}
				}
				Element author = new Element("Author");
				author.addContent(new Element("CollectiveName").setText(articleRecord.getStr("CollectiveName")==""?"":articleRecord.getStr("CollectiveName")));
				authorList.addContent(author);
				article.addContent(authorList);
				article.addContent(new Element("PublicationType").setText(articleRecord.getStr("PublicationType")==""?"":articleRecord.getStr("PublicationType")));
				Element articleIdList = new Element("ArticleIdList");
				articleIdList.addContent(new Element("ArticleId").setText(articleRecord.getStr("DOI")==""?"":articleRecord.getStr("DOI")).setAttribute("IdType", "doi"));
				articleIdList.addContent(new Element("ArticleId").setText(articleRecord.getStr("URL")==""?"":articleRecord.getStr("URL")).setAttribute("IdType", "url"));
				article.addContent(articleIdList);
				// 查询摘要记录表
				List<Record> summaryList=Db.find("SELECT * FROM Wprim_Summary WHERE article_id=?",articleRecord.getInt("id"));
				String str2="";
				for(Record summaryRecord:summaryList){
					
					str2+=summaryRecord.getStr("Title")+":"+summaryRecord.getStr("Content");
				}
				article.addContent(new Element("Abstract").setText(str2));
				Element keywordsList = new Element("KeywordsList");
				// 特殊值keyword处理
				String str3 = articleRecord.getStr("Keywords");

				if (str3 != null) {
					
					String[] keywords = str3.split(";");
					
					for (int k = 0; k < keywords.length; k++) {
						
						keywordsList.addContent(new Element("Keywords").setText(keywords[k]));
					
					}
				}	

				article.addContent(keywordsList);

				Element meSHList = new Element("MeSHList");
				// 处理MeSH
				String str4 = articleRecord.getStr("MeSH");

				if (str4 != null) {

					String[] MeSHList = str4.split(";");
					
					for (int l = 0; l < MeSHList.length; l++) {
						
						meSHList.addContent(new Element("MeSH").setText(MeSHList[l]));
						
					}
					
				} 
				article.addContent(meSHList);
				// 给父节点ArticleSet添加子节点Article
				root.addContent(article);
			}
			// 生成XML文档
			XMLOutputter xmlOut = new XMLOutputter(Format.getPrettyFormat());
			try {
				xmlOut.output(doc, new FileOutputStream("D:\\AtricleXML.xml"));
				// 导出记录记入表Wprim_Export_Log中
				Record log = Db.findFirst("SELECT TOP 1 * FROM WSSARTICLE WHERE crtime>? ORDER BY crtime DESC",exportLog.getStr("export_time"));
				Record record = new Record().set("export_time",log.getStr("crtime"));
				Db.save("Wprim_Export_Log", record);
			} catch (Exception e) {
				System.err.println("XML文档生成失败...");
			}
		}else{
			System.err.println("没有新的论文生成...");
		}
		
	}

}

生成的XML文档内容:

<?xml version="1.0" encoding="UTF-8"?>
<!--CreateTime:2016-05-06 17:31:28.787-->
<!DOCTYPE ArticleSet SYSTEM "WPRIM_2.dtd">
<ArticleSet>
  <Article>
    <Journal>
      <Country>China</Country>
      <PublisherName>Publisher</PublisherName>
      <JournalTitle>JournalTitleNO.1ByDarker</JournalTitle>
      <Issn>2000-2000</Issn>
      <Volume />
      <Issue>10</Issue>
      <PubDate>
        <Year>2016</Year>
        <Month>05</Month>
        <Day>05</Day>
      </PubDate>
    </Journal>
    <ArticleTitle>Article TitleNO.4</ArticleTitle>
    <VernacularTitle>Vernacular TitleNO.4</VernacularTitle>
    <FirstPage>30</FirstPage>
    <LastPage>40</LastPage>
    <Language>Spanish</Language>
    <AuthorList>
      <Author>
        <FirstName>First1111</FirstName>
        <MiddleName>Middle1111</MiddleName>
        <LastName>Last1111</LastName>
        <Affiliation>Affiliation1111</Affiliation>
      </Author>
      <Author>
        <FirstName>First2222</FirstName>
        <MiddleName>Middle2222</MiddleName>
        <LastName>Last2222</LastName>
        <Affiliation>Affiliation2222</Affiliation>
      </Author>
      <Author>
        <FirstName>First3333</FirstName>
        <MiddleName>Middle3333</MiddleName>
        <LastName>Last3333</LastName>
        <Affiliation>Affiliation3333</Affiliation>
      </Author>
      <Author>
        <CollectiveName>CollectiveName444</CollectiveName>
      </Author>
    </AuthorList>
    <PublicationType>Publication Type444</PublicationType>
    <ArticleIdList>
      <ArticleId IdType="doi">ArticleId-DOI4444</ArticleId>
      <ArticleId IdType="url">http://www.baidu.com</ArticleId>
    </ArticleIdList>
    <Abstract>Abstract Title1111:words1111
Abstract Title2222:words2</Abstract>
    <KeywordsList>
      <Keywords>keywords1111</Keywords>
      <Keywords>keywords2222</Keywords>
    </KeywordsList>
    <MeSHList>
      <MeSH>MeSH33</MeSH>
      <MeSH>MeSH44</MeSH>
    </MeSHList>
  </Article>
  <Article>
    <Journal>
      <Country>China</Country>
      <PublisherName>Publisher</PublisherName>
      <JournalTitle>JournalTitleNO.1ByDarker</JournalTitle>
      <Issn>2000-2000</Issn>
      <Volume />
      <Issue>10</Issue>
      <PubDate>
        <Year>2016</Year>
        <Month>05</Month>
        <Day>05</Day>
      </PubDate>
    </Journal>
    <ArticleTitle>Article TitleNO.3</ArticleTitle>
    <VernacularTitle>Vernacular TitleNO.3</VernacularTitle>
    <FirstPage>20</FirstPage>
    <LastPage>30</LastPage>
    <Language>French</Language>
    <AuthorList>
      <Author>
        <FirstName>First111</FirstName>
        <MiddleName>Middle111</MiddleName>
        <LastName>Last111</LastName>
        <Affiliation>Affiliation111</Affiliation>
      </Author>
      <Author>
        <CollectiveName>CollectiveName333</CollectiveName>
      </Author>
    </AuthorList>
    <PublicationType>Publication Type333</PublicationType>
    <ArticleIdList>
      <ArticleId IdType="doi">ArticleId-DOI333</ArticleId>
      <ArticleId IdType="url">http://www.baidu.com</ArticleId>
    </ArticleIdList>
    <Abstract>Abstract Title111:words111</Abstract>
    <KeywordsList>
      <Keywords>keywords111</Keywords>
    </KeywordsList>
    <MeSHList>
      <MeSH>MeSH33</MeSH>
    </MeSHList>
  </Article>
 </ArticleSet>

企鹅:61489385

你可能感兴趣的:(JDOM.XML)