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系列方式

猜你喜欢

转载自blog.csdn.net/weixin_38500325/article/details/81429691