JDOM不是java官方支持的解析方法,所以我们要导入jar包 jar包下载
用JDOM来解析xml的步骤:
1):创建SAXBuilder对象
2):创建一个输入流,将xml文件加载到流中
3):通过SAXBuilder对象的build方法,将输入流加载到SAXBuilder中
4):通过Document对象来获取xml文件的根节点
5):然后获取根节点下的子节点集合,开始进行遍历。
所有的代码加注释:
xml文件:
<?xml version="1.0" encoding="UTF-8"?>
<bookstore>
<book id="1">
<name>c</name>
<year>2014</year>
<price>89</price>
</book>
<book id="2">
<name>数据结构</name>
<year>2004</year>
<price>89</price>
<language>English</language>
</book>
</bookstor
实体类:
package com.xml.enty;
public class Book {
private String id;
private String name;
private String year;
private String author;
private String language;
private String price;
public String getId() {
return id;
}
public void setId(String id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
public String getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
}
测试类:
package com.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.xml.enty.Book;
public class JDOMTest {
public static void main(String[] args) {
ArrayList<Book> booklist=new ArrayList<Book>();
//1.创建一个SAXBuilder的对象
SAXBuilder saxbuilder=new SAXBuilder();
try {
//2.创建一个输入流,将xml文件加载到流中
InputStreamReader isr=new InputStreamReader(new FileInputStream("books.xml"),"UTF-8");
//3.通过saxbuilder的build方法,将输入流加载到SAXBuilder中
Document document=(Document)saxbuilder.build(isr);
//通过Document对象获取对象xml文件的根节点
Element rootElement=document.getRootElement();
//获取根节点下的子结点的List集合
List<Element> bookList=rootElement.getChildren();
for(Element element:bookList){
Book book=new Book();
System.out.println("---开始解析第"+(bookList.indexOf(element)+1)+"本书");
//解析节点的属性
List<Attribute> attributes=element.getAttributes();
//遍历节点中的属性
for(Attribute att:attributes){
book.setId(att.getValue());
System.out.print("第"+(attributes.indexOf(att)+1)+"的属性名为:"+att.getName());
System.out.println("-->属性值为:"+att.getValue());
}
//遍历节点中的子结点
List<Element> NodeList=element.getChildren();
for(Element node:NodeList){
System.out.print("节点名为:"+node.getName());
System.out.println("-->节点值为:"+node.getValue());
if(node.getName().equals("name")){
book.setName(node.getValue());
}
else if(node.getName().equals("author")){
book.setAuthor(node.getValue());
}
else if(node.getName().equals("year")){
book.setYear(node.getValue());
}
else if(node.getName().equals("price")){
book.setPrice(node.getValue());
}
else if(node.getName().equals("language")){
book.setLanguage(node.getValue());
}
}
booklist.add(book);
book=null;
System.out.println("=====结束解析第"+(bookList.indexOf(element)+1)+"本书=====");
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
for(Book books:booklist){
System.out.println(books.getId());
System.out.println(books.getName());
System.out.println(books.getYear());
System.out.println(books.getPrice());
System.out.println(books.getLanguage());
System.out.println(books.getAuthor());
System.out.println("---finish---");
}
}
}