<rt id="bn8ez"></rt>
<label id="bn8ez"></label>

  • <span id="bn8ez"></span>

    <label id="bn8ez"><meter id="bn8ez"></meter></label>

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

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

    dom4j 修改 xml

    Posted on 2009-07-28 21:54 Gavin.lee 閱讀(358) 評論(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"));
        }

    }

    主站蜘蛛池模板: 亚洲沟沟美女亚洲沟沟| 亚洲国产婷婷六月丁香| www国产亚洲精品久久久日本| 免费人成在线观看网站品爱网日本| 亚洲国产人成精品| 国产亚洲人成网站在线观看不卡| 中文字幕亚洲免费无线观看日本| 久久精品亚洲AV久久久无码| 亚洲AV日韩AV永久无码色欲| 国产免费人成视频尤勿视频| 91精品国产免费久久国语蜜臀 | 国产精品亚洲va在线观看| 免费人成在线观看播放a| 大地资源在线资源免费观看| 真人做A免费观看| 亚洲高清一区二区三区| 理论秋霞在线看免费| a成人毛片免费观看| 免费三级毛片电影片| 亚洲国产一区二区视频网站| 国产1024精品视频专区免费 | 成人性生交大片免费看无遮挡| 国产精品成人四虎免费视频| 亚洲欧洲日产国码av系列天堂| 亚洲综合激情另类小说区| 亚洲精品无码日韩国产不卡av| 国产精品美女久久久免费| 91九色老熟女免费资源站| 免费国内精品久久久久影院| 亚洲人成在线播放网站岛国| 亚洲欧美日韩中文高清www777| 99精品全国免费观看视频..| 18禁无遮挡无码网站免费| 亚洲中文字幕无码久久综合网| 亚洲va精品中文字幕| eeuss影院免费92242部| 一二三四影视在线看片免费| 亚洲自偷自偷图片| 亚洲日韩一区精品射精| 国产日韩一区二区三免费高清| 日本特黄特色aa大片免费|