位置:首頁 > Java技術 > Java XML教學 > Java JDOM解析器 - 解析XML文檔

Java JDOM解析器 - 解析XML文檔

使用JDOM的步驟

以下是解析時使用JDOM解析器文檔的步驟。

  • 導入XML相關的軟件包

  • 創建一個SAXBuilder

  • 從文件或流創建一個文檔

  • 提取根元素

  • 檢查屬性

  • 檢查子元素

導入XML相關的軟件包

import java.io.*;
import java.util.*;
import org.jdom2.*;

創建DocumentBuilder

SAXBuilder saxBuilder = new SAXBuilder();

從文件或流創建一個文檔

File inputFile = new File("input.txt");
SAXBuilder saxBuilder = new SAXBuilder();
Document document = saxBuilder.build(inputFile);

提取根元素

Element classElement = document.getRootElement();

檢查屬性

//returns specific attribute
getAttribute("attributeName"); 

檢查子元素

//returns a list of subelements of specified name
getChildren("subelementName"); 
//returns a list of all child nodes
getChildren(); 
//returns first child node
getChild("subelementName"); 

演示示例

這是輸入需要解析xml文件:

<?xml version="1.0"?>
<class>
   <student rollno="393">
      <firstname>dinkar</firstname>
      <lastname>kad</lastname>
      <nickname>dinkar</nickname>
      <marks>85</marks>
   </student>
   <student rollno="493">
      <firstname>Vaneet</firstname>
      <lastname>Gupta</lastname>
      <nickname>vinni</nickname>
      <marks>95</marks>
   </student>
   <student rollno="593">
      <firstname>jasvir</firstname>
      <lastname>singn</lastname>
      <nickname>jazz</nickname>
      <marks>90</marks>
   </student>
</class>

 

演示示例:

DomParserDemo.java

import java.io.File;
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 JDomParserDemo {
   public static void main(String[] args) {
      try {
         File inputFile = new File("input.txt");

         SAXBuilder saxBuilder = new SAXBuilder();

         Document document = saxBuilder.build(inputFile);

         System.out.println("Root element :" 
            + document.getRootElement().getName());

         Element classElement = document.getRootElement();

         List<Element> studentList = classElement.getChildren();
         System.out.println("----------------------------");

         for (int temp = 0; temp < studentList.size(); temp++) {    
            Element student = studentList.get(temp);
            System.out.println("\nCurrent Element :" 
               + student.getName());
            Attribute attribute =  student.getAttribute("rollno");
            System.out.println("Student roll no : " 
               + attribute.getValue() );
            System.out.println("First Name : " + student.getChild("firstname").getText());
            System.out.println("Last Name : "+ student.getChild("lastname").getText());
            System.out.println("Nick Name : "+ student.getChild("nickname").getText());
            System.out.println("Marks : "+ student.getChild("marks").getText());	            		
         }
      }catch(JDOMException e){
         e.printStackTrace();
      }catch(IOException ioe){
         ioe.printStackTrace();
      }
   }
}

這將產生以下結果:

Root element :class
----------------------------

Current Element :student
Student roll no : 393
First Name : dinkar
Last Name : kad
Nick Name : dinkar
Marks : 85

Current Element :student
Student roll no : 493
First Name : Vaneet
Last Name : Gupta
Nick Name : vinni
Marks : 95

Current Element :student
Student roll no : 593
First Name : jasvir
Last Name : singn
Nick Name : jazz
Marks : 90