需求:
① 给定XML文档,要求将数据拿出后,写入XML文档中
XML文档的形式为
<serviceApp id="" lastTime="" serviceAddress='' transfersRuleId='' >
<front id='' frontCode='' frontName=''>
</front>
<node id='' nodeCode='' nodeName='' nodeIp='' userName='' password='' port='' nodeBaseData='' nodeExample='' dataBaseOwer='' >
</node>
<table id='' tableName=''>
</table>
<targeFront frontCode='' frontName=''>
</targeFront>
<targeNode id='' nodeCode='' nodeName='' nodeIp='' userName='' password='' port='' nodeBaseData='' nodeExample='' dataBaseOwer=''>
</targeNode>
<targeTable id='' tableName=''>
</targeTable>
<findSql>
select * from table
</findSql>
<transfersRule id='' ruleName=''>
{}
</transfersRule>
</serviceApp>
① 首先,是生成XML文档的例子,(代码中的一些service方法是调用系统service层的,为了获取相应的数据,可以不予以理会)
采用的为 dom4j 的技术
可能需要的包
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
思路:生成一个XML类似于在 js中生成一个 dom树一样,确定根节点,然后像根节点中增加属性 和 子节点
从调用service层方法,获取相应的需要的值(可以忽略,只是为了方便下面代码)
//根节点IexchangeSercviceApplyService
@Autowired
private IexchangeSercviceApplyService IchangeServerApp=SpringUtils.getBean(IexchangeSercviceApplyService.class);
@Autowired
private IexchangeSercviceApplyDateService IchangeServiceDate=SpringUtils.getBean(IexchangeSercviceApplyDateService.class);
@Autowired
private IexchangeServiceManageService IchangeServiceMng=SpringUtils.getBean(IexchangeServiceManageService.class);
@Autowired
private IexchangeFrontManageService IchangeFrontServiceMng=SpringUtils.getBean(IexchangeFrontManageService.class);
@Autowired
private IexchangeNodeManageService ichangeNodeMage=SpringUtils.getBean(IexchangeNodeManageService.class);
private static String num=ConnectFactory.getProperties("DATA_NUM");
//FILED_URL=D:/Coding/Iexchange_spance/iexchange/src/main/java/com/cicro/iexchange/common/XMLFileHandling/
private static String fileurl=ConnectFactory.getProperties("FILED_URL");
/**
* 将数据写到XML文件中
* @param id。 xml文件中根节点的id属性。
* serviceApp id == iexchange_sercvice_apply
* return int型,若为1说明成功,若为0则失败。
* IexchangeSercviceApply
*/
public int writeXml(String id) {
int returnValue = 0;
try{
if(id==null && "".equals(id)){
System.out.println("传进来的AppId为空!");
return returnValue;
}else{
/**
* 获取根节点下面的所有属性值
*/
//根据所传进来的id,得到整条App信息
IexchangeSercviceApply IApp = IchangeServerApp.getById(id);
//根据Appid查询整条IexchangeSercviceApplyDate信息
IexchangeSercviceApplyDate iAppdate = IchangeServiceDate.findByAppId(id);
//得到根节点中lasttime属性
String lastTime = iAppdate.getLastTime();
//得到serviceAddress值
String serviceAddress = IApp.getIexchangeServiceManage().getServiceAddress();
//得到siteSave的值
String siteSave = IApp.getSiteSave();
if(siteSave==null){
siteSave = "";
}
//得到useState的值
String useState = IApp.getUseState();
if(useState==null){
useState = "";
}
//得到state的值
String state = IApp.getState();
if(state==null){
state = "";
}
//得到serviceStrategy的值
String serviceStrategy = IApp.getServiceStrategy();
if(serviceStrategy==null){
serviceStrategy = "";
}
//得到pastTime 的值
String pastTime = iAppdate.getFrontTime().toString();
if(pastTime==null){
pastTime = "";
}
/**
* 获取front节点下面的所有属性值
*/
//获得前置id
String frontid = IApp.getIexchangeFrontManage();
IexchangeFrontManage Front = IchangeFrontServiceMng.getById(frontid);
//得到frontCode的值
String frontCode = Front.getFrontCode();
if(frontCode==null){
frontCode = "";
}
//得到frontName的值
String frontName = Front.getFrontName();
if(frontName==null){
frontName = "";
}
/**
* 获取node节点下面的所有属性值
*/
//获得nodeId
String nodeId = iAppdate.getNodeId();
//得到nodemanager实体
IexchangeNodeManage nodemage = ichangeNodeMage.getById(nodeId);
//得到nodeCode的值
String nodeCode = nodemage.getNodeCode();
if(nodeCode==null){
nodeCode = "";
}
//得到nodeName的值
String nodeName = nodemage.getNodeNames();
if(nodeName==null){
nodeName = "";
}
//得到nodeIp的值
String nodeIp = nodemage.getNodeIp();
if(nodeIp==null){
nodeIp = "";
}
//得到password的值
String password = nodemage.getDatebasePassword();
if(password==null){
password = "";
}
//得到port的值
String port = nodemage.getDatebasePort();
if( port==null){
port = "";
}
//得到nodeBaseData的值
String nodeBaseData = nodemage.getNodeDatebase();
if(nodeBaseData==null){
nodeBaseData = "";
}
//得到dataBaseOwer的值
String dataBaseOwer = nodemage.getDatebaseTablespace();
if(dataBaseOwer==null){
dataBaseOwer = "";
}
/**
* 获取targeFront节点下面的所有属性值
*/
//获得targeFront 的id
String targeFrontid = IApp.getIexchangeFrontManageTarge();
//得到nodemanager实体
IexchangeFrontManage targeFront = IchangeFrontServiceMng.getById(targeFrontid);
//得到frontCode的值
String targefrontCode = targeFront.getFrontCode();
if(targefrontCode==null){
targefrontCode = "";
}
//得到frontName的值
String targefrontName = targeFront.getFrontName();
if(targefrontName==null){
targefrontName = "";
}
/**
* 获取targeNode节点下面的所有属性
*/
//获得nodeId
String targenodeId = iAppdate.getTargeNodeId();
//得到nodemanager实体
IexchangeNodeManage targenodemage = ichangeNodeMage.getById(targenodeId);
//得到nodeCode的值
String targenodeCode = targenodemage.getNodeCode();
if(targenodeCode==null){
targenodeCode = "";
}
//得到nodeName的值
String targenodeName = targenodemage.getNodeNames();
if(targenodeName==null){
targenodeName = "";
}
//得到nodeIp的值
String targenodeIp = targenodemage.getNodeIp();
if(targenodeIp==null){
targenodeIp = "";
}
//得到password的值
String targepassword = targenodemage.getDatebasePassword();
if(targepassword==null){
targepassword = "";
}
//得到port的值
String targeport = targenodemage.getDatebasePort();
if(targeport==null){
targeport = "";
}
//得到nodeBaseData的值
String targenodeBaseData = targenodemage.getNodeDatebase();
if(targenodeBaseData==null){
targenodeBaseData = "";
}
//得到dataBaseOwer的值
String targedataBaseOwer = targenodemage.getDatebaseTablespace();
if(targedataBaseOwer==null){
targedataBaseOwer = "";
}
/**
* 获得findSql
*/
GetTransmissionData data = new GetTransmissionData();
String sql = data.selectSql(IApp, nodemage, lastTime, num);
if(sql==null){
sql = "";
}
根据上面得到的值,生成一个XML文档。生成后记得关闭流。
/**
* 生成XML文件
*/
Document document = DocumentHelper.createDocument();
document.setXMLEncoding("GBK");
/** 建立XML文档的根serviceApp */
Element serviceAppElement = document.addElement("serviceApp");
/** 向XML文档的根serviceApp中增加属性 */
serviceAppElement.addAttribute("id", id);
serviceAppElement.addAttribute("lastTime", lastTime);
serviceAppElement.addAttribute("serviceAddress", serviceAddress);
serviceAppElement.addAttribute("siteSave", siteSave);
serviceAppElement.addAttribute("useState", useState);
serviceAppElement.addAttribute("state", state);
serviceAppElement.addAttribute("serviceStrategy", serviceStrategy);
serviceAppElement.addAttribute("pastTime", pastTime);
/** 加入第一个front节点 */
Element frontElement = serviceAppElement.addElement("front");
/** 向front节点中增加属性 */
frontElement.addAttribute("frontCode", frontCode);
frontElement.addAttribute("frontName", frontName);
/** 加入第一个node节点 */
Element nodeElement = serviceAppElement.addElement("node");
/** 向node节点中增加属性 */
nodeElement.addAttribute("nodeCode", nodeCode);
nodeElement.addAttribute("nodeName", nodeName);
nodeElement.addAttribute("nodeIp", nodeIp);
nodeElement.addAttribute("password", password);
nodeElement.addAttribute("port", port);
nodeElement.addAttribute("nodeBaseData", nodeBaseData);
nodeElement.addAttribute("dataBaseOwer", dataBaseOwer);
/** 加入第一个targeFront节点 */
Element targeFrontElement = serviceAppElement.addElement("targeFront");
/** 向targeFront节点中增加属性 */
targeFrontElement.addAttribute("frontCode", targefrontCode);
targeFrontElement.addAttribute("frontName", targefrontName);
/** 加入第一个targenode节点 */
Element targenodeElement = serviceAppElement.addElement("node");
/** 向node节点中增加属性 */
targenodeElement.addAttribute("nodeCode", targenodeCode);
targenodeElement.addAttribute("nodeName", targenodeName);
targenodeElement.addAttribute("nodeIp", targenodeIp);
targenodeElement.addAttribute("password", targepassword);
targenodeElement.addAttribute("port", targeport);
targenodeElement.addAttribute("nodeBaseData", targenodeBaseData);
targenodeElement.addAttribute("dataBaseOwer", targedataBaseOwer);
/** 加入第一个findSql节点 */
Element findSqlElement = serviceAppElement.addElement("findSql");
/** 向targeFront节点中增加属性 */
findSqlElement.addText(sql);
System.out.println("节点中的sql为:"+sql);
try{
/** 将document中的内容写入文件中 */
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("GBK");
XMLWriter writer = new XMLWriter(new FileWriter(new File(fileurl+id+".xml")),format);
//打到控制台
XMLWriter writer2 = new XMLWriter( System.out, format );
writer.write( document );
//输入到控制台
writer2.write(document);
//关闭流
writer.close();
}catch(Exception ex){
ex.printStackTrace();
}