博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
java--xml文件读取(JDOM&DOM4J)
阅读量:7166 次
发布时间:2019-06-29

本文共 4780 字,大约阅读时间需要 15 分钟。

1、JDOM解析

首先导入额外的jar包:

Build Path:jdom-2.0.6.jar

准备工做获取到子节点的集合:

package com.imooc_xml.jdom.text;import java.io.FileInputStream;import java.io.FileNotFoundException;import java.io.IOException;import java.io.InputStream;import java.util.List;import org.jdom2.Document;import org.jdom2.Element;import org.jdom2.JDOMException;import org.jdom2.input.SAXBuilder;public class JDOMTest {    public static void main(String[] args) {        //        1、创建一个SAXBuilder对象        SAXBuilder  saxBuilder = new SAXBuilder();        InputStream in ;        try {//            2、创建一个输入流,将xml文件加载到输入流中            in = new FileInputStream("xml/books.xml");//            3、通过saxBuilder的build方法,将输入流加载到saxBuilder中            Document document = saxBuilder.build(in);//            4、通过document对象获取xml文件的根节点            Element rootElement = document.getRootElement();//            5、获取根节点下的子节点的list集合            List
bookList = rootElement.getChildren(); } catch (FileNotFoundException e) { e.printStackTrace(); }catch (JDOMException | IOException e) { e.printStackTrace(); } }}

 获取节点的属性名和属性值,节点名和值:

for (Element book:bookList){                System.out.println("开始解析第"+(bookList.indexOf(book)+1)+"本书");                List
attrList = book.getAttributes(); for (Attribute attr : attrList){ attr.getName(); attr.getValue(); } //book.getAttributeValue("id"); List
bookChildren = book.getChildren(); for(Element children : bookChildren){ children.getName(); children.getValue(); } }

乱码的处理:

 使用InputStreamReader()指定编码

在JDOM中存储Book对象

private static ArrayList
bookList = new ArrayList
();//进行进一步解析 for (Element book:bookList){ Book bookEntity = new Book();for (Attribute attr : attrList){ String attrName = attr.getName(); String attrValue = attr.getValue(); if(attrName.equals("id")){ bookEntity.setId(attrValue); } }for(Element children : bookChildren){ String name = children.getName(); String value = children.getValue(); if(name.equals("name")){ bookEntity.setName(value); }else if(name.equals("author")){ bookEntity.setAuthor(value); }else if(name.equals("year")){ bookEntity.setYear(value); }else if(name.equals("price")){ bookEntity.setPrice(value); }else if(name.equals("language")){ bookEntity.setLanguage(value); } } JDOMTest.bookList.add(bookEntity); bookEntity=null;

 jar包随项目导入导出方法:

创建lib文件夹,复制jar包到此文件

build path添加jar包

2、DOM4J解析xml文件

导入jar包:

dom4j-1.6.1.jar

package com.imooc_xml.dom4j.test;import java.io.File;import java.util.Iterator;import java.util.List;import org.dom4j.Attribute;import org.dom4j.Document;import org.dom4j.DocumentException;import org.dom4j.Element;import org.dom4j.io.SAXReader;public class DOM4JTest {    public static void main(String[] args) {        SAXReader reader = new SAXReader();        try {            Document document = reader.read(new File("xml/books.xml"));            Element bookstore = document.getRootElement();            Iterator it = bookstore.elementIterator();            while (it.hasNext()) {                Element book = (Element) it.next();                List
bookAttrs = book.attributes(); for(Attribute attr : bookAttrs){ attr.getName(); attr.getValue(); } Iterator itt = book.elementIterator(); while (itt.hasNext()) { Element bookChild = (Element) itt.next(); bookChild.getName(); bookChild.getStringValue(); } } } catch (DocumentException e) { e.printStackTrace(); } }}

四种解析方式的分析:

 DOM:

。与平台无关的官方解析方式

*需要的内存性能要高一点

SAX:

。java平台提供的一种基于事件驱动的解析方式

*解析图解

每走一部触发不同的方法

JDOM、DOM4J:

。在DOM、SAX方法上扩展的,只有Java中能够使用的解析方法

*

 性能测试:

build path-->add Library-->JUnit

@Test  public void testPerformance() throws Exception{
     long start = System.currentTimeMillis(); //测试方法的性能 long end = System.currentTimeMillis(); System.out.println(start-end);

 

转载于:https://www.cnblogs.com/Nyan-Workflow-FC/p/6412588.html

你可能感兴趣的文章