dom4j解析XML(二)

修改文档  

  

这一节说明如何使用 dom4j API 修改示例 XML 文档 catalog.xml。  

  

使用 SAXReader 解析 XML 文档 catalog.xml:  

  

SAXReader saxReader = new SAXReader();  

Document document = saxReader.read(inputXml);   

  

  

SAXReader 包含在 org.dom4j.io 包中。   

  

inputXml 是从 c:/catalog/catalog.xml 创建的 java.io.File。使用 XPath 表达式从 article 元素中获得 level 节点列表。如果 level 属性值是“Intermediate”则改为“Introductory”。   

  

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" );   

        }   

  

  

获取 article 元素列表,从 article 元素中的 title 元素得到一个迭代器,并修改 title 元素的文本。   

  

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" );  

     }}   

  

  

通过和 title 元素类似的过程修改 author 元素。   

  

清单 4 所示的示例程序 Dom4JParser.java 用于把 catalog.xml 文档修改成 catalog-modified.xml 文档。  

  

清单 4 . 用于修改 catalog.xml 的程序(Dom4Jparser.java)   

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" ));  

                                         }  

    }  

你可能感兴趣的:(java,C++,c,xml,C#)