随笔-10  评论-23  文章-32  trackbacks-0

1.新建一个class类,用来生成xml文件,并输出到本地磁盘,内容如下:

import java.io.FileWriter;
import java.io.IOException;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;
public class createXml {
      public static Document getDocument(){          
                  Document document = DocumentHelper.createDocument(); 
                  //生成一个接点 
                  Element root = document.addElement("root"); 
                  //生成root的一个接点 
                 Element category = root.addElement("category"); 
                 //生产category的一个接点 
                 Element id = category.addElement("id"); 
                 //生成id里面的参数值 
                 id.addAttribute("name", "id"); 
                 //生成id里面的值 
                 id.addText("1"); 
                 return document; 
             }
               /**
              * 写入xml文件地址
              * @param document 组合好的xml document
              * @param outFile 文件存放的地址
              */ 
      public static void writeDocument(Document document, String outFile){ 
                 try{ 
                     //读取文件 
                     FileWriter fileWriter = new FileWriter(outFile); 
                     //设置文件编码 
                     OutputFormat xmlFormat = new OutputFormat(); 
                     xmlFormat.setEncoding("UTF-8"); 
                     //创建写文件方法 
                     XMLWriter xmlWriter = new XMLWriter(fileWriter,xmlFormat); 
                     //写入文件 
                     xmlWriter.write(document); 
                     //关闭 
                     xmlWriter.close(); 
                 }catch(IOException e){ 
                     System.out.println("文件没有找到"); 
                     e.printStackTrace(); 
                 } 
             } 

      public static void main(String[] args){
         createXml.writeDocument(createXml.getDocument(), "f:\\a.xml"); 

    }
}

右键执行以上代码,会在F盘下生成一个a.xml文件

如下图:

12373585720

 

2.新建一个类,用于解析和修改刚才生成的xml文件。

import java.io.File;
import java.io.FileWriter;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.util.Iterator;
import java.util.List;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.SAXReader;
import org.dom4j.io.XMLWriter;
public class CatchXml {
    public CatchXml(){
    }
    @SuppressWarnings("unchecked")
    public void changeXml(File inputxml){
        try {
            SAXReader reader = new SAXReader();
            Document doc = reader.read(inputxml);
            List list = doc.selectNodes("//category");
            Iterator iter = list.iterator();
            while(iter.hasNext()){
                Element element = (Element)iter.next();
                element.addComment("it is a comment for liuyimx!");
                Iterator iterator = element.elementIterator("id");
                while(iterator.hasNext()){
                    Element IdElement = (Element)iterator.next();
                    if(IdElement.getText().equals("1")){
                        IdElement.setText("liuyi");
                    }
                    if(IdElement.attribute("name").getValue().equals("id")){
                        IdElement.attribute("name").setValue("001");
                    }
                }
            }
            OutputFormat format = new OutputFormat("  ",true);
            format.setEncoding("UTF-8");
            XMLWriter writer = new XMLWriter(new FileWriter(inputxml));
            writer.write(doc);
            writer.close();
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
        catch (DocumentException e) {
            e.printStackTrace();
        }
    }
    public static void main(String args[]){
        File file = new File("f:\\a.xml");
        CatchXml cx = new CatchXml();
        cx.changeXml(file);
    }
}

右键执行,修改后a.xml如下:

12373675950 

依赖jar包:

dom4j-1.6.1.jar

jaxen-1.1.1.jar

posted on 2009-08-04 15:36 liuyimx 阅读(2552) 评论(0)  编辑  收藏 所属分类: dom4jxml

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


网站导航: