目前看到的对XML的处理有2套,一套是使用jdom,另一套是使用dom4j
jdom示例:
SAXBuilder builder = new SAXBuilder(false); InputStream xmlInput = new FileInputStream("xxx.xml"); Document doc = builder.build(xmlInput); Element root = doc.getRootElement(); Element addDTSVersion = new Element("VersionA"); addDTSVersion.setText(configDTSVersion); Element addrallyVersion = new Element("VersionB"); addrallyVersion.setText(configRallyVersion); Element addTime = new Element("Maping"); addTime.addContent(addDTSVersion); addTime.addContent(addrallyVersion); root.addContent(addTime);
List<Element> Mapinglist = root.getChildren("Maping");
XMLOutputter outputter = new XMLOutputter();try {String locaFileName = ConfigUtils.class.getResource("/"+fileName).toString();locaFileName = locaFileName.substring(6);outputter.output(doc,new FileOutputStream(locaFileName));} catch (FileNotFoundException e) {e.printStackTrace();} catch (IOException e) {e.printStackTrace();}