package sample; import java.io.IOException; import java.util.List; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; public class Sample2 { /** * @param args * @throws IOException * @throws JDOMException */ public static void main(String[] args) throws JDOMException, IOException { // TODO Auto-generated method stub SAXBuilder sb=new SAXBuilder(); Document doc=sb.build("zip.xml"); Element root =doc.getRootElement(); List<Element> lst=root.getChildren("property"); for(Element el :lst) { if(el.getAttributeValue("name").equals("mubiaowenjian")) el.setAttribute("value", "sunzekun"); }//找寻属性名name为mulu的子元素 修改value属性 SaveXml.saveXML(doc); Runtime.getRuntime().exec("cmd /k ant -f zip.xml"); //通过ant执行build.xml System.out.println("chenggong"); } }
package sample; import java.io.FileWriter; import java.io.IOException; import org.jdom.Document; import org.jdom.output.Format; import org.jdom.output.XMLOutputter; public class SaveXml { public static void saveXML(Document doc) throws IOException{ XMLOutputter xmlopt=new XMLOutputter(); //创建xml文件输出流 FileWriter writer= new FileWriter("zip.xml"); //构建输出对象到zip.xml Format fm = Format.getPrettyFormat(); xmlopt.setFormat(fm); //指定文档格式 xmlopt.output(doc, writer); //将doc写入到指定的文件夹中 writer.close(); } }
<?xml version="1.0" encoding="UTF-8"?> <project name="compress" default="zip"> <property name="mulu" value="e:/ant+jdom/xutianhao"/> <property name="mubiaowenjian" value="xutianhao"/> <target name="zip"> <zip destfile="${mubiaowenjian}.zip" basedir="${mulu}" /> </target> </project>