无线&移动互联网技术研发

换位思考·····
posts - 19, comments - 53, trackbacks - 0, articles - 283
  BlogJava :: 首页 :: 新随笔 :: 联系 :: 聚合  :: 管理

dom4j 修改 xml

Posted on 2009-07-28 21:54 Gavin.lee 阅读(345) 评论(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"));
    }

}


只有注册用户登录后才能发表评论。


网站导航: