将xml文件导入到oracle数据库
importjava.io.File;
importjava.util.ArrayList;
importjava.util.Iterator;
importjava.util.List;
importorg.apache.commons.beanutils.BeanUtils;
importorg.dom4j.Document;
importorg.dom4j.Element;
importorg.dom4j.io.SAXReader;
importcom.lbs.commons.ClassHelper;
importcom.lbs.commons.TransManager;
importcom.lbs.commons.op.OPException;
importcom.lbs.commons.op.OPManager;
importcom.start.lemis.apps.commons.code.People;
importcom.start.lemis.apps.commons.code.PeopleDTO;
/**
*将xml文件导入到oracle数据库
*<p>
*Description:
*</p>
*<p>
*Copyright:Copyright(c)2008
*</p>
*Company:实达科技
*
*@authorlvrl
*@version1.0
*/
publicclassXmlPaser{
/**
*@paramfileName
*@paramformatStyle
*@paramresultType
*@return
*/
publicListuploadXml(StringfileName,StringformatStyle,ClassresultType){
ListallData=newArrayList();
try{
//dom4j解析器
SAXReadersaxReader=newSAXReader();
//读取文件文档
Documentdocument=saxReader.read(newFile(fileName));
//根据xml各类标示获取所有类集合
Listlist=document.selectNodes(formatStyle);
if(list!=null);
{for(Iteratorit=list.iterator();it.hasNext();)
{
Elementelement=(Element)it.next();
ListelementList=element.elements();
//返回实体类型
ObjectreturnInstance=resultType.newInstance();
for(inti=0;i<elementList.size();i++)
{
Elementdata=(Element)elementList.get(i);
BeanUtils.setProperty(returnInstance,data.getName().toLowerCase(),data.getData());
}
allData.add(returnInstance);
}
}
}catch(Exceptione){
//TODO:handleexception
}
returnallData;
}
publicstaticvoidmain(String[]arg)
{
TransManagertm=newTransManager();
//标记是否启动事务
intflag=0;
ListallData=null;
ListallPeople=newArrayList();
XmlPasertemp=newXmlPaser();
allData=temp.uploadXml("C://people.xml","/PEOPLE/PERSON",PeopleDTO.class);
if(allData!=null){
for(Iteratorit=allData.iterator();it.hasNext();)
{
Peoplepeople=newPeople();
PeopleDTOdto=(PeopleDTO)it.next();
ClassHelper.copyProperties(dto,people);
allPeople.add(people);
}
}
OPManagerop=newOPManager();
try{
flag=tm.begin();
op.saveObjs(allPeople.toArray());
//操作成功,提交
if(1==flag)
{
tm.commit();
System.out.println("成功导入数据");
}
}catch(OPExceptione){
//TODO自动生成catch块
e.printStackTrace();
}
}
}