Java 使用JDOM解析XML文檔

使用JDOM解析XML文檔

        JDOM是一種使用 XML(標準通用標記語言下的一個子集) 的獨特 Java 工具包。下面咱們來看一下怎麼使用
JDOM來解析XML文檔。java

        首先下載JDOM的jar包,並導入到工程中。具體的操做方法這裏再也不贅述了,大佬們必定不會在意這個東西。
下面就在說一說具體的操做步驟吧。dom

     1 . 建立一個SAXBuilder對象。工具

SAXBuilder saxBuilder = new SAXBuilder();

     2 . 調用SAXBuilder對象的build()方法。(Document對象是org.jdom2.Document包下的)ui

Document document = saxBuilder.build("book.xml");

     3 . 調用document對象的getRootElement()方法獲取根節點。code

Element element = document.getRootElement();

    4 . 調用Element對象的getChildren()方法獲取根節點的子節點。xml

List<Element> books = element.getChildren();

    5 . 調用getAttributes()方法或者getAttributeValue("id")方法獲取節點屬性。對象

String value = book.getAttributeValue("id");
//List<Attribute> bookAttrs = book.getAttributes();

    6 . 調用getChildren()方法或者getChild()方法再次得到其子節點。element

List<Element> bookChildren = book.getChildren();

下面是一個簡單的實例,供你們參考。文檔

book.xmlget

<?xml version="1.0" encoding="UTF-8" ?>
<bookstore>
    <book id="1">
        <name>111</name>
        <author>222</author>
    </book>
    <book id="2">
        <name>333</name>
        <author>444</author>
    </book>
</bookstore>

jdom.java

package com.lpp.test;

import java.io.IOException;
import java.util.List;

import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;

public class jdom
{

    public static void main(String[] args)
    {
        // TODO Auto-generated method stub  

        SAXBuilder saxBuilder = new SAXBuilder();

        try
        {
            Document document = saxBuilder.build("book.xml");   

            Element element = document.getRootElement();

            List<Element> books = element.getChildren();

            for (Element book : books)
            {
                System.out.println("開始解析!第" + (books.indexOf(book) + 1) + "本");

                // String value = book.getAttributeValue("id");
                // System.out.println("id:"+value);
                List<Attribute> bookAttrs = book.getAttributes();
                for (Attribute bookAttr : bookAttrs)// 和上面的效果同樣
                {
                    String attrName = bookAttr.getName();
                    String attrValue = bookAttr.getValue();
                    System.out.println("屬性" + attrName + ":" + attrValue);
                }   

                List<Element> bookChildren = book.getChildren();
                for (Element bookChild : bookChildren)
                {
                    String bookChildElementName = bookChild.getName();
                    String bookChildElementValue = bookChild.getValue();    

                    System.out.println("節點" + bookChildElementName + ":" + bookChildElementValue);
                }
                System.out.println("結束解析!第" + (books.indexOf(book) + 1) + "本");
            }

        } catch (JDOMException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e)
        {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }   

}
相關文章
相關標籤/搜索