Dom4j操作xml

package com.kk.xml;

import java.io.*;
import java.util.*;

import org.dom4j.*;
import org.dom4j.io.*;

public class OperateXML {

    /**
     * @param args
     * @throws IOException 
     * @throws DocumentException 
     */
    public static void main(String[] args) throws IOException, DocumentException {
        generateXML();
        parseXMLFromString();
    }

    /**
     * 将生成的xml以字符串形式打印
     * @throws IOException
     */
    private static void generateXML() throws IOException {
        Document document=DocumentHelper.createDocument();
        Element message=document.addElement("message");
        message.addAttribute("type", "GetAllDefence");
        Element students=message.addElement("students");
        for(int i=0;i<10;i++){
            Element student=students.addElement("student");
            student.addElement("name").setText("my name is"+i);
            student.addElement("gender").setText("男");
        }
        System.out.println(document.asXML());
    }
    
    /**
     * 将生成的xml以字符串形式打印(字节流)
     * @throws IOException
     */
    private static void generateXMLByStream() throws IOException {
        Document document=DocumentHelper.createDocument();
        Element message=document.addElement("message");
        message.addAttribute("type", "GetAllDefence");
        Element students=message.addElement("students");
        for(int i=0;i<10;i++){
            Element student=students.addElement("student");
            student.addElement("name").setText("my name is"+i);
            student.addElement("gender").setText("男");
        }
        OutputStream os=new ByteArrayOutputStream();
        OutputFormat format=OutputFormat.createPrettyPrint();
        format.setEncoding("utf-8");
        XMLWriter output=new XMLWriter(os,format);
        output.write(document);
        output.close();
        String str=os.toString();
        os.close();
        System.out.println(str);        
    }
    
    private static void parseXMLFromFile() throws DocumentException{
        SAXReader reader=new SAXReader();
        Document document=reader.read("students.xml"); //读流
        Element root=document.getRootElement(); //message
        //for(Iterator it=root.selectNodes("//students/student").iterator();it.hasNext();){ //xpath方式,需要jaxen.jar
        for(Iterator it=root.element("students").elementIterator("student");it.hasNext();){
            Element student=(Element) it.next();
            System.out.println(student.elementTextTrim("name"));
            System.out.println(student.elementTextTrim("gender"));
        }
    }
    
    private static void parseXMLFromString() throws DocumentException{
        String xml="<?xml version=\"1.0\" encoding=\"gbk\"?>"
                    +"<message type=\"GetAllDefence\">"
                    +"<students>"
                    +"<student num=\"0\">"
                    +"<name>my name is0</name>"
                    +"<gender>男</gender>"
                    +"</student>"
                    +"</students>"
                    +"</message>";
        Document document=DocumentHelper.parseText(xml); //读字符串
        Element root=document.getRootElement(); //message
        for(Iterator it=root.element("students").elementIterator("student");it.hasNext();){
            Element student=(Element) it.next();
            System.out.println(student.elementTextTrim("name"));
            System.out.println(student.elementTextTrim("gender"));
        }
    }

}
posted @ 2012-07-20 11:44  microsoft_kk  阅读(1231)  评论(0编辑  收藏  举报