JDOM读取XML文件例子程序
来源:互联网 发布:js下拉菜单视频教程 编辑:程序博客网 时间:2024/05/22 03:36
转自:http://www.mkyong.com/java/how-to-read-xml-file-in-java-jdom-example/
How To Read XML File In Java – (JDOM Parser)
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 : yongLast Name : mook kimNick Name : mkyongSalary : 100000First Name : lowLast Name : yin fongNick Name : fong fongSalary : 200000
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)
- JDOM读取XML文件例子程序
- Jdom 读取 xml文件例子
- JDom读取xml文件例子
- jdom读取XML文件
- jdom读取xml文件
- jdom读取xml文件
- Jdom读取XML文件
- jdom读取xml文件
- jdom读取xml文件
- jdom 读取xml文件
- Jdom读取XML文件
- Jdom读取xml文件
- 【jdom】读取xml文件
- [Java] JDOM 读取 xml 文件 示例程序初步
- 用jdom读取xml文件
- 利用jdom读取xml文件
- 通过JDom读取XML文件
- 用JDOM读取XML文件
- Android Http 异步请求
- 向SD卡写文件,需要添加写权限
- SWT重写窗体最小化到磁盘
- python获取某网址下所有图片
- Windows Phone 7 多点触控理论
- JDOM读取XML文件例子程序
- 在Visual Studio 中替换空行
- Android ViewPager多页面滑动切换以及动画效果
- 在网站后台使用uploadify上传图片遇到问题HTTP Error (302)
- Apache Shiro 简介
- easyui tree使用方法
- Java 线程同步之原子变量
- linux bash shell之declare
- php---数据库类封装