码迷,mamicode.com
首页 > 其他好文 > 详细

dom4j读写xml简单demo

时间:2015-02-05 16:22:20      阅读:174      评论:0      收藏:0      [点我收藏+]

标签:dom   xml   dom4j   

package com.protocol;

import java.io.*;
import java.util.List;
import org.dom4j.*;
import org.dom4j.io.*;

public class Dom4jDemo {

    public void createXml(String fileName) {

        DocumentFactory factory = new DocumentFactory();
        Document document = factory.createDocument();
        Element root = document.addElement("root");
        for(Integer i=0; i < 2; ++i)
        {
            Element book = root.addElement("book");
            book.addAttribute("id",i.toString());
            book.addAttribute("name","bookname");

            Element author = book.addElement("author");
            author.setText("dalangge");

            Element price = book.addElement("price");
            price.setText("15");
        }

        try {
            FileWriter fw = new FileWriter(fileName);
            OutputFormat format = new OutputFormat("  ",true,"gb2312");
            XMLWriter writer = new XMLWriter(fw,format);
            writer.write(document);
            fw.close();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public void parserXml(String fileName) {
        try {
            File f = new File(fileName);
            InputStream in = new FileInputStream(f);
            SAXReader reader = new SAXReader();
            Document doc = reader.read(in);
            Element root = doc.getRootElement();
            readNode(root);
        } catch (DocumentException e) {
            e.printStackTrace();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }
    }

    @SuppressWarnings("unchecked")
    public void readNode(Element root) {
        if (root == null) return;

        System.out.println(root.getName()+":"+(String)root.getData());

        // 获取属性
        List<Attribute> attrs = root.attributes();
        if (attrs != null && attrs.size() > 0) {
            for (Attribute attr : attrs) {
                System.out.println(attr.getName() + ": " + attr.getValue());
            }
        }
        // 获取他的子节点
        List<Element> childNodes = root.elements();
        for (Element e : childNodes) {
            readNode(e);
        }
    }



}

dom4j读写xml简单demo

标签:dom   xml   dom4j   

原文地址:http://blog.csdn.net/iamgongqinghua/article/details/43529455

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!