java 将字符串写到xml某节点中

config.ini


[eyoowon]
actionparam = update\_eyoowon.exe
ver = 1.0.0.3
name = eyoowon.zip
action = 3
md5 = 
businesses =

1.xml

<< config  checkverinterval =" 60 "  username =" kb123 "  host =" update.51cy.org "  ver =" 1354252970 "  port =" 81 " >
< app  ver =" 1.0.0.4 "  md5 =" " >
   < update  action =" 3 "  actionparam =" update\ "  cmd =" http://update.51cy.org:81/client/client.zip $(appdir)\update\client\client.zip "  name =" update\client\client.zip "  />
   app >
< businesses >
< business  ver =" "  name =" $(appdir)\temp\AdShower.exe "  active =" 0 "  parameter =" "  md5 =" dc411261f79fc8137164166f6f965641 " >
   < update  action =" 3 "  actionparam =" $(appdir)\temp "  cmd =" http://update.51cy.org:81/package/adshower.zip $(appdir)\update\package\adshower.zip " name =" update\package\adshower.zip "  />
   business >
   businesses >
   < files  />
   config >


从config.ini文件中读取businesses的值放到1.xml的businesses节点中

Element businesses = null;

Document docu = DocumentHelper.parseText(business); // 将字符串转为XML
 Element rootElt = docu.getRootElement(); // 获取根节点
if(root.element("businesses")==null){
businesses=root.addElement("businesses");
}
businesses.add(rootElt);

ServletContext sc = ServletActionContext.getServletContext();
String path = sc.getRealPath("1.xml");
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("UTF-8");
FileOutputStream fos=new FileOutputStream(new File(path));
XMLWriter writer=new XMLWriter(fos,format);
writer.write(root);
writer.close();



你可能感兴趣的:(javaEE)