JDOM Example : Reading and Parsing XML with SAX parser in Java

XML parsing with JDOM parser
JDOM is an open source library which allow XML parsing and reading in Java program. JDOM is designed by using Java programming technique and customized for Java programmers, so that Java programmer with very little knowledge of XML documents can use JDOM to read XML files. Unlike DOM Parser of Java API , which uses Factory design pattern to create instance of parser e.g DocumentBuilderFactory and DocumentBuilder, as seen in our last example of parsing XML documents in Java, JDOM uses new() operator to create its parser instances. In fact JDOM is very easy to understand and most of the time its self explanatory. While trying this Java program to parse XML file, you can explore JDOM API. JDOM API also allows to use XPATH expression to query XML documents with package org.jdom2.xpath. In next section we will see complete code example of How to read an XML file using JDOM parser in Java program.

How to read XML file using JDOM in Java

How to read XML file using JDOM parser in Java exampleIn this code example we will use following XML file to demonstrate How JDOM read this XML file and provide access to root, various elements and attributes.


<?xml version = "1.0" encoding = "UTF-8" ?>
<books>
<book
ISBN = "1234" >
<author>
James </author>
<title>
Few words </title>
<pages>
120 </pages>
<language>
English </language>
</book>
<book
ISBN = "5678" >
<author>
Peter </author>
<title>
Around the World </title>
<pages>
200 </pages>
<language>
English </language>
</book>
</books>

and here is a Java program which reads this XML document using JDOM library :

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

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

/**
* Java program to read XML file using JDOM library.
* JDOM is an open source library which is close to Java style
* and allows programmer to read and write XML files in JAva
*
* @author
*/


public class XMLReader {

public staticvoid main( String args []){

//creating JDOM SAX parser
SAXBuilder builder = new SAXBuilder () ;

//reading XML document
Document xml = null ;
try {
xml = builder. build ( new File ( "test.xml" )) ;
} catch (JDOMException e ) {
e. printStackTrace () ;
} catch ( IOException e ) {
e. printStackTrace () ;
}

//getting root element from XML document
Element root = xml. getRootElement () ;

System. out. println ( "Root element of XML document is : " + root. getName ()) ;
System. out. println ( "Number of books in this XML : " + root. getChildren (). size ()) ;

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

//Iterating over all childs in XML
Iterator < Element > itr = books. iterator () ;
while (itr. hasNext ()){
Element book = itr. next () ;
//reading attribute from Element using JDOM
System. out. println ( "ISBN : " + book. getAttributeValue ( "ISBN" )) ;

//reading value of childern in XML using JDOM
System. out. println ( "author : " + book. getChildText ( "author" )) ;
System. out. println ( "title : " + book. getChildText ( "title" )) ;
System. out. println ( "pages : " + book. getChildText ( "pages" )) ;
System. out. println ( "language : " + book. getChildText ( "language" )) ;
}
}
}

Output:
Root element of XML document is : books
Number of books in this XML : 2
ISBN : 1234
author : James
title : Few words
pages : 120
language : English
ISBN : 5678
author : Peter
title : Around the World
pages : 200
language : English

If you look at this example of parsing XML document using JDOM parser, you will find it surprisingly simple. you just need to create SAXBuilder and then you got Document object, from that you can get Element and correspondingly value of child and attributes.

That's all on How to read XML file using JDOM open source library. JDOM is very easy to use library for Java programmer and great for quickly reading XML documents in Java.
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值