首页 > 代码库 > SaxReader读取,更新xml文件

SaxReader读取,更新xml文件

package com.sun.xml;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStreamReader;
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 ReadXmlBySaxReader {
    public static void main(String[] args) throws IOException {
        String path="C:\\Users\\Administrator\\Desktop\\bookstore.xml";
        Document document=getDocument(path);
        getNode(document);
        updateEle("aaaa", document,path);
        addEle(document, "编程书籍", path);
    }
    public static Document getDocument(String path) throws UnsupportedEncodingException, FileNotFoundException{
        SAXReader saxReader=new SAXReader();
        File file=new File(path);
        Document document=null;
        InputStreamReader inputStreamReader=new InputStreamReader(new FileInputStream(file));
        try {
            document=saxReader.read(inputStreamReader);
            return document;
        } catch (DocumentException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        return null;
    }
    
    public static void getNode(Document document){
        Element element=document.getRootElement();
        @SuppressWarnings("unchecked")
        List<Element> list=element.elements("book");
        Iterator<Element> iterator=list.iterator();
        while(iterator.hasNext()){
            Element node=iterator.next();
            Element element2=node.element("title");
            System.out.println(element2.getText());
        }
        System.out.println("ss");
    }
    public static void updateEle(String text,Document document,String path) throws IOException{
        Element element=document.getRootElement();
        @SuppressWarnings("unchecked")
        List<Element> list=element.elements("book");
        Iterator<Element> iterator=list.iterator();
        while(iterator.hasNext()){
            Element node=iterator.next();
            Element element2=node.element("title");
            System.out.println("title为:   "+element2.getText());
            if(element2.getText().equals("ss")){
                element2.setText("java书籍");
            }
            
        }
        writeXml(document,path);
    }
    
    
    public static void addEle(Document document,String text,String path) throws IOException{
        Element rootElement=document.getRootElement();
        Element element=rootElement.addElement("book");
        Element element2=element.addElement("title");
        Element element3=element.addElement("author");
        Element element4=element.addElement("price");
        element2.setText(text);
        element3.setText("万福");
        element4.setText("39.0");
        writeXml(document, path);
    }
    public static void writeXml(Document document,String path) throws IOException{
        OutputFormat outputFormat=OutputFormat.createPrettyPrint();
        
        try {
            XMLWriter xmlWriter=(XMLWriter) new XMLWriter(new FileOutputStream(path),outputFormat);
            xmlWriter.write(document);
            xmlWriter.close();
        } catch (UnsupportedEncodingException | FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }
}


本文出自 “matengbing” 博客,请务必保留此出处http://matengbing.blog.51cto.com/11395502/1876374

SaxReader读取,更新xml文件