JDOM Example : Reading and Parsing XML with SAX parser in Java
来源:互联网 发布:淘宝注册网店步骤费用 编辑:程序博客网 时间:2024/04/29 12:49
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 ofparsing 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 completecode example ofHow to read an XML file using JDOM parser in Java program.
How to read XML file using JDOM in Java
In 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.
<?xmlversion="1.0"encoding="UTF-8"?>
<books>
<bookISBN="1234">
<author>James</author>
<title>Few words</title>
<pages>120</pages>
<language>English</language>
</book>
<bookISBN="5678">
<author>Peter</author>
<title>Around the World</title>
<pages>200</pages>
<language>English</language>
</book>
</books>
<books>
<bookISBN="1234">
<author>James</author>
<title>Few words</title>
<pages>120</pages>
<language>English</language>
</book>
<bookISBN="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 :
importjava.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 staticvoidmain(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
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 staticvoidmain(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.
- JDOM Example : Reading and Parsing XML with SAX parser in Java
- parsing XML using DOM, SAX and StAX Parser in Java
- parsing xml with sax and pull
- Step By Step guide to Read XML file in Java Using SAX Parser Example
- Parsing XML Files with SAX
- How To Read XML File In Java – (SAX Parser)
- Java Reading XML using DOM parser
- Android: Simplified source code for parsing and working with XML data and web services in Android
- Java XML :DOM, SAX, JDOM,JAXB
- JAVA解析xml DOM SAX JDOM DOM4J
- read XML - SAX parser
- How to use Comparator and Comparable in Java? With example
- Android: Reading, using and working with XML data and web services in Android
- Example Code of Using XML Parser in Symbian OS
- sax,jdom解析xml
- 【Java编程】SAX XML Parser解析、生成XML文件
- 【Java编程】SAX XML Parser解析、生成XML文件
- XML processing with java (SAX)
- 我遇到的Nginx+uwsgi的500错误解决记录
- Java线程中断的本质和编程原则
- Linxu环境下使用Eclipse直接调试mapreduce程序(不用插件)
- Ubuntu--The package system is broken
- 基于UDT connect连接通信以及文件传输--客户端
- JDOM Example : Reading and Parsing XML with SAX parser in Java
- MFC OnOk(),OnCancel(),OnClose(),OnDestroy()的区别总结
- 生产者消费者模式C++程序模拟实现
- Difference between TreeMap and TreeSet in Java
- 黑马程序员-学习笔记java基础-高级类特性
- Can we Override Private Method in Java? Inner Class?
- Java字节码指令集
- Visual Studio 十五个开发技巧
- QueryRunner使用