無線&移動互聯網技術研發

          換位思考·····
          posts - 19, comments - 53, trackbacks - 0, articles - 283
            BlogJava :: 首頁 :: 新隨筆 :: 聯系 :: 聚合  :: 管理

          dom4j 修改 xml

          Posted on 2009-07-28 21:54 Gavin.lee 閱讀(363) 評論(0)  編輯  收藏 所屬分類: xml doc 操作
          來自IBM的文章,感覺不錯,剛開始使用XPath的時候,不知道還要jaxen.jar包,郁悶了好半天,呵

          //    使用dom4j解析XML時,要快速獲取某個節點的數據,使用XPath是個不錯的方法,dom4j的快速手冊里也建議使用這種方式
          //    執行時卻拋出以下異常:
          //
          //    Exception in thread "main" java.lang.NoClassDefFoundError: org/jaxen/JaxenException
          //    at org.dom4j.DocumentFactory.createXPath(DocumentFactory.java:230)
          //    at org.dom4j.tree.AbstractNode.createXPath(AbstractNode.java:207)
          //    at org.dom4j.tree.AbstractNode.selectNodes(AbstractNode.java:164)
          //
          //    不光要有dom4j這個包,還要有jaxen包:<jaxen-1.1-beta-6.jar>-238 KB,這應該是dom4j的基礎包,在dom4j的zip包的lib目錄里可以找到。即使用這個方法需要以下兩個包:
          //    dom4j-1.6.1.jar-306 KB
          //    jaxen-1.1-beta-6.jar-238 KB

          修改前
          <?xml version="1.0" encoding="UTF-8"?> 
          <catalog> 
          <!--An XML Catalog--> 
          <?target instruction?>
            
          <journal title="XML Zone" 
                            publisher
          ="IBM developerWorks"> 
          <article level="Intermediate" date="December-2001">
           
          <title>Java configuration with XML Schema</title> 
           
          <author> 
               
          <firstname>Marcello</firstname> 
               
          <lastname>Vitaletti</lastname> 
           
          </author>
            
          </article>
            
          </journal> 
          </catalog>

          修改后
          <?xml version="1.0" encoding="UTF-8"?> 
          <catalog> 
          <!--An XML catalog--> 
          <?target instruction?>
            
          <journal title="XML Zone"
                             publisher
          ="IBM developerWorks"> 
          <article level="Introductory" date="October-2002">
           
          <title>Create flexible and extensible XML schemas</title> 
           
          <author> 
               
          <firstname>Ayesha</firstname> 
               
          <lastname>Malik</lastname> 
           
          </author> 
            
          </article>
            
          </journal> 
          </catalog>

          解析器
          package com.Gavin.tools.xml;

          import java.io.File;
          import java.io.FileWriter;
          import java.io.IOException;
          import java.util.Iterator;
          import java.util.List;

          import org.dom4j.Attribute;
          import org.dom4j.Document;
          import org.dom4j.DocumentException;
          import org.dom4j.Element;
          import org.dom4j.io.SAXReader;
          import org.dom4j.io.XMLWriter;

          public class TestDom4jChange {
              
          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) {
                  TestDom4jChange dom4jParser 
          = new TestDom4jChange();
                  dom4jParser.modifyDocument(
          new File("c:/catalog/catalog.xml"));
              }

          }

          主站蜘蛛池模板: 遵义市| 福州市| 淮阳县| 青海省| 贡山| 土默特右旗| 社旗县| 理塘县| 靖边县| 和顺县| 苍南县| 舟山市| 固安县| 松原市| 榆树市| 襄垣县| 读书| 黔东| 墨玉县| 廉江市| 浙江省| 托克逊县| 攀枝花市| 东阿县| 循化| 建德市| 蓝田县| 利辛县| 镇坪县| 瑞金市| 赫章县| 神池县| 无棣县| 凤庆县| 灵丘县| 宁城县| 龙江县| 于都县| 潼南县| 涟源市| 西昌市|