使用 dom4j 解析 XML(四)
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Attribute;
import java.util.List;
import java.util.Iterator;
import org.dom4j.io.XMLWriter;
import java.io.*;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
public class Dom4JParser{
?public void modifyDocument(File inputXml){
? try{
?? SAXReader saxReader = new SAXReader();
?? Document document = saxReader.read(inputXml);
?? List list = document.selectNodes("//article/@level" );
?? Iterator iter=list.iterator();
?? while(iter.hasNext()){
??? Attribute attribute=(Attribute)iter.next();
??? if(attribute.getValue().equals("Intermediate"))
????? attribute.setValue("Introductory");
?????? }
??
?? list = document.selectNodes("//article/@date" );
?? iter=list.iterator();
?? while(iter.hasNext()){
??? Attribute attribute=(Attribute)iter.next();
??? if(attribute.getValue().equals("December-2001"))
????? attribute.setValue("October-2002");
?????? }
?? list = document.selectNodes("//article" );
?? iter=list.iterator();
?? while(iter.hasNext()){
??? Element element=(Element)iter.next();
??? Iterator iterator=element.elementIterator("title");
????? while(iterator.hasNext()){
??????? Element titleElement=(Element)iterator.next();
??????? if(titleElement.getText().equals("Java configuration with XML
????? Schema"))
??????? titleElement.setText("Create flexible and extensible XML schema");
????????????????????????????????????????? }
??????????????????????????????? }
??? list = document.selectNodes("//article/author" );
??? iter=list.iterator();
???? while(iter.hasNext()){
???? Element element=(Element)iter.next();
???? Iterator iterator=element.elementIterator("firstname");
???? while(iterator.hasNext()){
????? Element firstNameElement=(Element)iterator.next();
????? if(firstNameElement.getText().equals("Marcello"))
????? firstNameElement.setText("Ayesha");
???????????????????????????????????? }
????????????????????????????? }
??? list = document.selectNodes("//article/author" );
??? iter=list.iterator();
???? while(iter.hasNext()){
????? Element element=(Element)iter.next();
????? Iterator iterator=element.elementIterator("lastname");
???? while(iterator.hasNext()){
????? Element lastNameElement=(Element)iterator.next();
????? if(lastNameElement.getText().equals("Vitaletti"))
????? lastNameElement.setText("Malik");
????????????????????????????????? }
?????????????????????????????? }
???? XMLWriter output = new XMLWriter(
????? new FileWriter( new File("c:/catalog/catalog-modified.xml") ));
???? output.write( document );
???? output.close();
?? }
?
? catch(DocumentException e)
???????????????? {
????????????????? System.out.println(e.getMessage());
??????????????????????????? }
? catch(IOException e){
?????????????????????? System.out.println(e.getMessage());
??????????????????? }
?}
?public static void main(String[] argv){
? Dom4JParser dom4jParser=new Dom4JParser();
? dom4jParser.modifyDocument(new File("c:/catalog/catalog.xml"));
??????????????????????????????????????? }
?? }
节说明了如何使用 dom4j 中的解析器修改示例 XML 文档。这个解析器不使用 DTD 或者模式验证 XML 文档。如果 XML 文档需要验证
import org.dom4j.Document;
import org.dom4j.Element;
import org.dom4j.Attribute;
import java.util.List;
import java.util.Iterator;
import org.dom4j.io.XMLWriter;
import java.io.*;
import org.dom4j.DocumentException;
import org.dom4j.io.SAXReader;
public class Dom4JParser{
?public void modifyDocument(File inputXml){
? try{
?? SAXReader saxReader = new SAXReader();
?? Document document = saxReader.read(inputXml);
?? List list = document.selectNodes("//article/@level" );
?? Iterator iter=list.iterator();
?? while(iter.hasNext()){
??? Attribute attribute=(Attribute)iter.next();
??? if(attribute.getValue().equals("Intermediate"))
????? attribute.setValue("Introductory");
?????? }
??
?? list = document.selectNodes("//article/@date" );
?? iter=list.iterator();
?? while(iter.hasNext()){
??? Attribute attribute=(Attribute)iter.next();
??? if(attribute.getValue().equals("December-2001"))
????? attribute.setValue("October-2002");
?????? }
?? list = document.selectNodes("//article" );
?? iter=list.iterator();
?? while(iter.hasNext()){
??? Element element=(Element)iter.next();
??? Iterator iterator=element.elementIterator("title");
????? while(iterator.hasNext()){
??????? Element titleElement=(Element)iterator.next();
??????? if(titleElement.getText().equals("Java configuration with XML
????? Schema"))
??????? titleElement.setText("Create flexible and extensible XML schema");
????????????????????????????????????????? }
??????????????????????????????? }
??? list = document.selectNodes("//article/author" );
??? iter=list.iterator();
???? while(iter.hasNext()){
???? Element element=(Element)iter.next();
???? Iterator iterator=element.elementIterator("firstname");
???? while(iterator.hasNext()){
????? Element firstNameElement=(Element)iterator.next();
????? if(firstNameElement.getText().equals("Marcello"))
????? firstNameElement.setText("Ayesha");
???????????????????????????????????? }
????????????????????????????? }
??? list = document.selectNodes("//article/author" );
??? iter=list.iterator();
???? while(iter.hasNext()){
????? Element element=(Element)iter.next();
????? Iterator iterator=element.elementIterator("lastname");
???? while(iterator.hasNext()){
????? Element lastNameElement=(Element)iterator.next();
????? if(lastNameElement.getText().equals("Vitaletti"))
????? lastNameElement.setText("Malik");
????????????????????????????????? }
?????????????????????????????? }
???? XMLWriter output = new XMLWriter(
????? new FileWriter( new File("c:/catalog/catalog-modified.xml") ));
???? output.write( document );
???? output.close();
?? }
?
? catch(DocumentException e)
???????????????? {
????????????????? System.out.println(e.getMessage());
??????????????????????????? }
? catch(IOException e){
?????????????????????? System.out.println(e.getMessage());
??????????????????? }
?}
?public static void main(String[] argv){
? Dom4JParser dom4jParser=new Dom4JParser();
? dom4jParser.modifyDocument(new File("c:/catalog/catalog.xml"));
??????????????????????????????????????? }
?? }
节说明了如何使用 dom4j 中的解析器修改示例 XML 文档。这个解析器不使用 DTD 或者模式验证 XML 文档。如果 XML 文档需要验证