JDOM读取XML文件例子程序
2014-04-30 15:53
99 查看
转自:http://www.mkyong.com/java/how-to-read-xml-file-in-java-jdom-example/
Posted on December 21, 2009 , Last modified : August 4, 2011
By mkyong
JDOM is, quite simply, a Java representation of an XML document. JDOM provides a way to represent that document for easy and efficient reading, manipulation, and writing. It has
a straightforward API, is a lightweight and fast, and is optimized for the Java programmer. It’s an alternative to DOM and SAX, although it integrates well with both DOM and SAX.
JDOM, Java XML parser, is more user friendly in the way of accessing the XML document.
In this example, we show you how to use JDOM to read a XML file, and print out each element orderly.
JDOM is not like SAX or DOM, which bundled in JDK. To use JDOM, you need to download the library manually.
Get JDOM from JDOM official site or declares following dependency if you are using Maven.
3. Java File
Use JDOM parser to parse above XML file.
Output
Note
You may also at the following two examples :
How to read XML file in Java – (SAX Parser)
How to read XML file in Java – (DOM Parser)
How To Read XML File In Java – (JDOM Parser)
Posted on December 21, 2009 , Last modified : August 4, 2011
By mkyong
JDOM is, quite simply, a Java representation of an XML document. JDOM provides a way to represent that document for easy and efficient reading, manipulation, and writing. It has
a straightforward API, is a lightweight and fast, and is optimized for the Java programmer. It’s an alternative to DOM and SAX, although it integrates well with both DOM and SAX.
JDOM, Java XML parser, is more user friendly in the way of accessing the XML document.
In this example, we show you how to use JDOM to read a XML file, and print out each element orderly.
1. Download the JDOM library
JDOM is not like SAX or DOM, which bundled in JDK. To use JDOM, you need to download the library manually.Get JDOM from JDOM official site or declares following dependency if you are using Maven.
<dependency> <groupId>jdom</groupId> <artifactId>jdom</artifactId> <version>1.1</version> </dependency>
2. XML File
XML file as following<?xml version="1.0"?> <company> <staff> <firstname>yong</firstname> <lastname>mook kim</lastname> <nickname>mkyong</nickname> <salary>100000</salary> </staff> <staff> <firstname>low</firstname> <lastname>yin fong</lastname> <nickname>fong fong</nickname> <salary>200000</salary> </staff> </company>
3. Java File
Use JDOM parser to parse above XML file.
import java.io.File; import java.io.IOException; import java.util.List; import org.jdom.Document; import org.jdom.Element; import org.jdom.JDOMException; import org.jdom.input.SAXBuilder; public class ReadXMLFile { public static void main(String[] args) { SAXBuilder builder = new SAXBuilder(); File xmlFile = new File("c:\\file.xml"); try { Document document = (Document) builder.build(xmlFile); Element rootNode = document.getRootElement(); List list = rootNode.getChildren("staff"); for (int i = 0; i < list.size(); i++) { Element node = (Element) list.get(i); System.out.println("First Name : " + node.getChildText("firstname")); System.out.println("Last Name : " + node.getChildText("lastname")); System.out.println("Nick Name : " + node.getChildText("nickname")); System.out.println("Salary : " + node.getChildText("salary")); } } catch (IOException io) { System.out.println(io.getMessage()); } catch (JDOMException jdomex) { System.out.println(jdomex.getMessage()); } } }
Output
First Name : yong Last Name : mook kim Nick Name : mkyong Salary : 100000 First Name : low Last Name : yin fong Nick Name : fong fong Salary : 200000
Note
You may also at the following two examples :
How to read XML file in Java – (SAX Parser)
How to read XML file in Java – (DOM Parser)
相关文章推荐
- 另类保存微信公众平台历史消息的方法 - 星标消息
- 惊涛怪浪(double dam-break) -- position based fluids
- 异常
- 在Visual Studio 中替换空行
- android常用工具记录
- 在一台电脑访问另一台电脑的mysql数据库
- Android Http 异步请求
- hadoop源码 - Client代码结构
- error: pointer targets in passing argument 3 of 'accept' differ in signedness [-Werror=pointer-sign
- 实现运动的尾巴效果
- Reverse Words in a String
- 安装Oracle提示OracleMTSRecoveryService 已经存在,解决方法
- Upstart事件驱动的任务和服务引导方法简述
- Redis 配置文件详解
- uwsgi1.9.17的python开发环境配置
- [Unity3D]Unity3D游戏开发之2D贴图实现血条组件
- 集合框架
- Chap4: question: 19 - 28
- freemarker web.xml
- ubuntu系统修改系统最大打开文件数的步骤