Java DOM4J解析器 - 修改XML文檔

演示示例

這是我們需要修改的XML文檔:

<student rollno="393"> dinkar kad dinkar 85 <student rollno="493"> Vaneet Gupta vinni 95 <student rollno="593"> jasvir singn jazz 90

演示示例:

DOM4jModifyXMLDemo.java

package com.yiibai.xml; import java.io.File; import java.io.IOException; import java.io.UnsupportedEncodingException; import java.util.Iterator; import java.util.List; import org.dom4j.Document; import org.dom4j.DocumentException; import org.dom4j.Element; import org.dom4j.Node; import org.dom4j.io.OutputFormat; import org.dom4j.io.SAXReader; import org.dom4j.io.XMLWriter; public class DOM4jModifyXMLDemo { public static void main(String[] args) { try { File inputFile = new File("input.txt"); SAXReader reader = new SAXReader(); Document document = reader.read( inputFile ); Element classElement = document.getRootElement(); List<Node> nodes = document.selectNodes("/class/student[@rollno='493']" ); for (Node node : nodes) { Element element = (Element)node; Iterator<Element> iterator=element.elementIterator("marks"); while(iterator.hasNext()){ Element marksElement=(Element)iterator.next(); marksElement.setText("80"); } } // Pretty print the document to System.out OutputFormat format = OutputFormat.createPrettyPrint(); XMLWriter writer; writer = new XMLWriter( System.out, format ); writer.write( document ); } catch (DocumentException e) { e.printStackTrace(); } catch (UnsupportedEncodingException e) { e.printStackTrace(); } catch (IOException e) { e.printStackTrace(); } } }

這將產生以下結果:

dinkar kad dinkar 85 Vaneet Gupta vinni 80 jasvir singn jazz 90