日期:2014-05-19 浏览次数:20696 次
import java.io.File; import java.io.FileOutputStream; import java.io.IOException; import java.util.Iterator; import java.util.List; import org.dom4j.Document; import org.dom4j.Element; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; public class Test1 { public static void main(String[] args) { SAXReader reader = new SAXReader(); File file = new File("d:\\test.xml"); try { Document document = reader.read(file); List<Element> urls = document.selectNodes("abc/bc/url"); if(urls != null && urls.size() > 0){ for(Iterator it = urls.iterator();it.hasNext();){ Element url = (Element)it.next(); System.out.println(url.getText());//打印原值 url.setText("http://forum.csdn.net/");//修改 } } OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter output = new XMLWriter(new FileOutputStream(new File("d:\\test1.xml")), format); output.write(document); output.close(); } catch (Exception e) { e.printStackTrace(); } } }