首页 诗词 字典 板报 句子 名言 友答 励志 学校 网站地图
当前位置: 首页 > 教程频道 > 网站开发 > XML SOAP >

dom4j解析以及写下xml数据

2012-11-01 
dom4j解析以及写入xml数据public class Dom4jMain {/** * @param args */public static void main(String[

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();
}
}

}

热点排行