package com.primeton.eos;
import java.io.File;
import java.io.FileWriter;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
import com.eos.foundation.data.DataObjectUtil;
import com.eos.system.annotation.Bizlet;
import commonj.sdo.DataObject;
@Bizlet("")
public class Dom4jUtil {
@Bizlet("")
public static StringcastDataObjects2XML(DataObject[] daos)
throwsException {
String filePath ="c:/abc/test.xml";
Document doc =DocumentHelper.createDocument();
Element root =doc.addElement("dicts");//这是XML的根目录,在这里我写成了实体的复数名
Element category =null;//这是实体名
Element dicttypeid =null;
Element dictid = null;
Element dictname = null;
Element status = null;
Element sortno = null;
Element rank = null;
Element parentid = null;
Element seqno = null;
for (DataObject dao : daos){
category =root.addElement("dict");//添加实体
dicttypeid =category.addElement("dicttypeid");
if(dao.getString("eosDictType/dicttypeid") != null)
dicttypeid.addText(dao.getString("eosDictType/dicttypeid"));
dictid =category.addElement("dictid");
if(dao.getString("dictid") != null)
dictid.addText(dao.getString("dictid"));
dictname =category.addElement("dictname");
if(dao.getString("dictname") != null)
dictname.addText(dao.getString("dictname"));
status =category.addElement("status");
if(dao.getString("status") != null)
status.addText(dao.getString("status"));
sortno =category.addElement("sortno");
if(dao.getString("sortno") != null)
sortno.addText(dao.getString("sortno"));
rank =category.addElement("rank");
if(dao.getString("rank") != null)
rank.addText(dao.getString("rank"));
parentid =category.addElement("parentid");
if(dao.getString("parentid") != null)
parentid.addText(dao.getString("parentid"));
seqno =category.addElement("seqno");
if(dao.getString("seqno") != null)
seqno.addText(dao.getString("seqno"));
}
OutputFormat xmlFormat = newOutputFormat();//添加一个文件导出属性的实体
xmlFormat.setEncoding("GBK");//将导出文件的编码设置成GBK
filePath =filePath.replace("\\", "/");//将路径中的\替换成/
File file = newFile(filePath);
if(!file.exists())(newFile(filePath.substring(0,filePath.lastIndexOf("/")))).mkdirs();//如果是不存在的目录,则生成对应的目录
XMLWriter writer = newXMLWriter(new FileWriter(filePath),xmlFormat);//生成写XML文件的类
writer.write(doc);
writer.close();
return filePath;
}
@Bizlet("")
public static DataObject[]castXML2DataObject(String filePath) {
List lists = newArrayList();//定义一个List,因为返回的是数组,开始无法确定数组的长度,故先生成List
SAXReader reader = newSAXReader();//读取XML文件的类
try {
Document doc= reader.read(new File(filePath));
Element root= doc.getRootElement();//获取根节点
IteratoreleIterator = root.elementIterator();//获取实体集合
DataObjectdao = null;
while(eleIterator.hasNext()) {
Elementdict = eleIterator.next();
IteratordictIterator = dict.elementIterator();//获取属性集合
while(dictIterator.hasNext()) {
dao=DataObjectUtil.createDataObject("com.primeton.eos.dom4jutildb.EosDictEntry");
dao.set("eosDictType/dicttypeid",dictIterator.next().getText());
dao.set("dictid",dictIterator.next().getText());
dao.set("dictname",dictIterator.next().getText());
dao.set("status",dictIterator.next().getText());
dao.set("sortno",dictIterator.next().getText());
dao.set("rank",dictIterator.next().getText());
dao.set("parentid",dictIterator.next().getText());
dao.set("seqno",dictIterator.next().getText());
}
if(dao != null)lists.add(dao);
}
} catch (Exception e) {
e.printStackTrace();
}
return (DataObject[])lists.toArray(new DataObject[lists.size()]);
}
}