(1)导入JDOM的JAR包
(2)XML文件内容
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book id="1">
<name>冰与火之歌</name>
<author>乔治·马丁</author>
<year>2015</year>
<price>200</price>
</book>
<book id="2">
<name>光荣与梦想</name>
<year>2004</year>
<price>150</price>
<publish>科学出版社</publish>
</book>
</bookstore>
(3)JDOM解析方法
package com.myjavapractice.xml;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.ArrayList;
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;
import com.myjavapractice.xml.entity.Book;
public class JDOMTest {
private static ArrayList<Book> booksList = new ArrayList<Book>();
public static void main(String[] args) {
// 创建一个SAXBuilder对象
SAXBuilder saxbuilder = new SAXBuilder();
InputStream in;
try {
in = new FileInputStream("src/res/MyFirstXML.xml");
//InputStreamReader isr = new InputStreamReader(in, "UTF-8");
// 通过SAXBuilder对象的build方法创今年一个Document对象,将XML文件载入
Document document = saxbuilder.build(in);
// Document document = saxbuilder.build(isr);
// 通过Document对象的getRootElement方法获取XML文件的根节点
Element rootElement = document.getRootElement();
// 获取根节点下的所有子节点,用Element泛型的List来承接
List<Element> bookList = rootElement.getChildren();
// 遍历子节点列表,获取属性列表
for (Element book : bookList) {
//创建Book对象,边解析边存储
Book bookEntity = new Book();
int num1 = bookList.indexOf(book) + 1;
System.out.println("========开始解析第" + num1 + "本书的内容========");
List<Attribute> attribute = book.getAttributes();
// 遍历属性列表,获取属性名和值
// book.getAttributeValue("id"); 直接根据已知属性名来获取它的值
System.out.println("第" + num1 + "本书共有" + attribute.size()
+ "个属性:");
for (Attribute attr : attribute) {
int num2 = bookList.indexOf(book) + 1;
String attrName = attr.getName();
String attrValue = attr.getValue();
System.out.print("第" + num2 + "个属性的名称是:" + attrName);
System.out.println("---->它的属性值是:" + attrValue);
//存储属性
if(attrName.equals("id")){
bookEntity.setId(attrValue);
}
}
// 遍历获取所有子节点的名称和子节点的值
List<Element> bookChildren = book.getChildren();
for (Element child : bookChildren) {
String childName = child.getName();
String childValue = child.getValue();
System.out.println("节点名:" + childName + "---->节点值: "
+ childValue);
//存储节点值
if(childName.equals("name")){
bookEntity.setName(childValue);
}
if(childName.equals("author")){
bookEntity.setAuthor(childValue);
}
if(childName.equals("price")){
bookEntity.setPrice(childValue);
}
if(childName.equals("year")){
bookEntity.setYear(childValue);
}
if(childName.equals("publish")){
bookEntity.setPublish(childValue);
}
}
System.out.println("========结束解析第" + num1 + "本书的内容========");
booksList.add(bookEntity);
bookEntity = null;
System.out.println(booksList.size());
System.out.println(booksList.get(0).getName());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}