java--xml檔案讀取(JDOM&DOM4J)

來源:互聯網
上載者:User

標籤:long   jar包   java   saxreader   擷取   指定   匯出   document   輸入資料流   

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<Element> 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<Attribute> attrList = book.getAttributes();                for (Attribute attr : attrList){                    attr.getName();                    attr.getValue();                }                //book.getAttributeValue("id");                                List<Element> bookChildren = book.getChildren();                for(Element children : bookChildren){                    children.getName();                    children.getValue();                }            }

亂碼的處理:

 使用InputStreamReader()指定編碼

在JDOM中儲存Book對象

private static ArrayList<Book> bookList = new ArrayList<Book>();//進行進一步解析            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<Attribute> 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);

 

java--xml檔案讀取(JDOM&DOM4J)

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.