首先要下载JDOM.jar包,下载地址:http://download.csdn.net/detail/ww6055/8880371
下载到JDOM.jar包之后导入到工程中去。
实例程序:
books.xml文件:(注:books.xml文件在src/res文件夹下)
Book.java实体类:
public class Book {
private String id;
private String name;
private String price;
private String author;
private String year;
private String language;
public String getLanguage() {
return language;
}
public void setLanguage(String language) {
this.language = language;
}
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 getPrice() {
return price;
}
public void setPrice(String price) {
this.price = price;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getYear() {
return year;
}
public void setYear(String year) {
this.year = year;
}
}
SAXTest.java类实现对books.xml文件的解析(注:文件中需要导入的Attribute、Document、Element、JDOMException都是JDOM包下的):
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 java.util.Vector;
import org.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
import com.xml.Jdom.model.Book;
public class JdomTest {
//对books.xml文件进行JDOM解析
public static void main(String[] args) {
List<Book> booklist = new ArrayList<Book>();
//STEP1.创建一个SAXBuilder对象
SAXBuilder sBuilder = new SAXBuilder();
InputStream in;
try {
//STEP2.创建一个输入流,将xml文件加载到输入流中
in= new FileInputStream("src/res/books.xml");
//进一步包装成InputStreamReader对象,处理乱码现象
InputStreamReader isr = new InputStreamReader(in,"utf-8");
//STEP3.通过SAXBuilder对象的build方法,将输入流加载到saxBuilder中
Document doc=sBuilder.build(isr);
//STEP4.通过Document对象可以获取bookstore的根节点
Element ele = doc.getRootElement();
//STEP5.获取根节点下的子节点的集合
List<Element> bookList = ele.getChildren();
//继续解析
for(Element book:bookList){
System.out.println("============开始解析第"+(bookList.indexOf(book)+1)+"本书===============");
Book b = new Book();
//如果一直输姓名则可以直接获取属性值:System.out.println(book.getAttributeValue("id"));
//book下所有的属性
List<Attribute> attrList=book.getAttributes();
for(Attribute attr:attrList){
System.out.println("属性名是:"+attr.getName()+" ***** 属性值是:"+attr.getValue());
if(attr.getName().equals("id"))
b.setId(attr.getValue());
}
List<Element> list = book.getChildren();
for(Element element:list){
System.out.println("节点名:"+element.getName()+" ******* 节点值:"+element.getValue());
if(element.getName().equals("name"))
b.setName(element.getValue());
if(element.getName().equals("author"))
b.setAuthor(element.getValue());
if(element.getName().equals("year"))
b.setYear(element.getValue());
if(element.getName().equals("price"))
b.setPrice(element.getValue());
if(element.getName().equals("language"))
b.setLanguage(element.getValue());
}
booklist.add(b);
b=null;//把Book对象设置为空,防止出现属性设置现象
// System.out.println(book.getAttributes());
// System.out.println("节点值是:"+book.getValue());
System.out.println("============结束解析第"+(bookList.indexOf(book)+1)+"本书===============");
}
System.out.println(booklist.get(0).getName()+"****"+booklist.get(1).getName());
} catch (FileNotFoundException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}catch (JDOMException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}