JAVA SE 读写XML——JDOM

JDom

准备工作,先导入jar包

JDom.jar下载 http://www.jdom.org/downloads/

实例应用

package JDom;

import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import org.jdom2.output.Format;
import org.jdom2.output.XMLOutputter;

import java.io.*;
import java.util.ArrayList;
import java.util.List;

public class TestJDom {
    private static ArrayList<Book> bookslist = new ArrayList<Book>();

    public static void main(String[] args) {
        new TestJDom().createXml();
    }

    //解析xml
    public void xmlparse() {
        SAXBuilder saxBuilder = new SAXBuilder();
        InputStream inputStream;

        try {
            inputStream = new FileInputStream("books.xml");
            Document document = saxBuilder.build(inputStream);
            Element rootElement = document.getRootElement();
            rootElement.getChildren();
            List<Element> booklist = rootElement.getChildren();

            for (Element book : booklist) {
                Book bookEntity = new Book();
                List<Attribute> attrList = book.getAttributes();
                for (Attribute attribute : attrList) {
                    String attrName = attribute.getName();//属性名
                    String attrValue = attribute.getValue();//属性属性值
                    if (attrName.equals("id")) {
                        bookEntity.setId(attrValue);
                    }
                }
                //遍历子节点
                List<Element> bookChilds = book.getChildren();
                for (Element child : bookChilds) {
                    if (child.getName().equals("name")) {
                        bookEntity.setName(child.getValue());
                    } else if (child.getName().equals("author")) {
                        bookEntity.setAuthor(child.getValue());
                    } else if (child.getName().equals("year")) {
                        bookEntity.setYear(child.getValue());
                    } else if (child.getName().equals("price")) {
                        bookEntity.setPrice(child.getValue());
                    }
                }
                bookslist.add(bookEntity);
                bookEntity = null;
            }
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (JDOMException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    //生成xml
    public void createXml() {
        Element rss = new Element("rss");
        rss.setAttribute("version", "2.0");
        Document document = new Document(rss);
        Element channel = new Element("channel");
        rss.addContent(channel);
        Element title = new Element("title");
        title.setText("<![CDATA[特殊字符]]>");
        channel.addContent(title);
        Format format = Format.getPrettyFormat();
        XMLOutputter outputter = new XMLOutputter(format);
        try {
            outputter.output(document, new FileOutputStream(new File("rss.xml")));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
}

 

相关文章——读写xml系列方式

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值