dom4j解析以及写入xml数据

public class Dom4jMain {

/**
* @param args
*/
public static void main(String[] args) {
SAXReader reader = new SAXReader();   
File file = new File("E:\\apache-tomcat\\webapps\\AAA\\resources.xml");
try {
Document doc = reader.read(file);
Element root = doc.getRootElement();
for(Iterator i = root.elementIterator("resource"); i.hasNext();){
Element ele = (Element)i.next();
for(Iterator iter = ele.elementIterator(); iter.hasNext();){
Element element = (Element)iter.next();
System.out.println(element.getData());
if(element.getText().equals("000100")){
//删除element
root.remove(ele);
}
}
}

//添加element
// Element comment = root.addElement("resource");
// comment.addElement("id").addText("000100");

FileWriter writer = new FileWriter(file);
OutputFormat format = OutputFormat.createPrettyPrint();
format.setEncoding("utf-8");
XMLWriter xmlWriter = new XMLWriter(writer,format);  
            xmlWriter.write(doc);  
            xmlWriter.close();  
            writer.close();  
} catch (DocumentException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}

}

你可能感兴趣的:(apache,tomcat,xml)