要被解析的xml文件对象
<?xml version="1.0" encoding="UTF-8" ?>
<bookstore>
<book id="1">
<name>冰与火之歌</name>
<author>乔治马丁</author>
<year>2014</year>
<price>89</price>
</book>
<book id="2">
<name>安徒生童话</name>
<author>安徒生</author>
<year>2017</year>
<price>77</price>
<language>Chinese</language>
</book>
</bookstore>
源代码:
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.jdom.Attribute;
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.JDOMException;
import org.jdom.input.SAXBuilder;
public class JDOMTest {
//以JDOM的方式来对xml文件对象进行解析操作,注:要以jdom方式来对xml文件进行解析时必须要先导入一个jdom.jar包
private static ArrayList<Book> booksList=new ArrayList<Book>();//建立一个book集合对象用于对所解析出来的book对象数据进行保存操作
public static void main(String[] args) {
//对book.xml 文件对象进行解析操作
//1:创建一个SAXBuilder实例对象
SAXBuilder saxBuilder=new SAXBuilder();
//2:创建一个输入流对象用于将xml文件对象当中的数据读取到输入流当中去
InputStream in;
try {
in = new FileInputStream("xml文件/books.xml");
InputStreamReader isr=new InputStreamReader(in,"utf-8");/*对输入流对象当中所对取到的xml文件进行重新编码后将其读取到InputStreamReader当中去,
这样可以防止乱码的产生*/
//3:通过saxBuilder对象的build方法来将输入流当中的数据信息加载到saxbuilder对象当中去
Document document=saxBuilder.build(isr);
//4:通过document对象来获取xml文件对象当中的根节点对象
Element rootElement=document.getRootElement();//获取当前xml文件对象当中的根节点元素对象 rootElement--->bookstore
//5:根据根节点对象来获取其全部的子节点对象
List<Element> bookList=rootElement.getChildren();//当前集合对象当中存放着根节点对象当中的全部子节点对象,当前集合对象当中存放着两个book节点对象
//继续进行解析.对集合对象当中的数据信息进行遍历,(使用与不知道当前book标签对象当中的属性名和数量时所使用的)
for(Element book : bookList)
{
System.out.println("==========开始解析第"+(bookList.indexOf(book)+1)+"本书========");
Book bookEntity=new Book();//建立一个book对象的实体,用于对所解析出来book标签对象当中的数据信息进行保存操作
List<Attribute> attributes=book.getAttributes();//获取当前book对象当中的属性集合
//对当前book标签对象当中的所有的属性值进行遍历操作
for(Attribute attr : attributes)
{
//获取当前标签对象当中的属性名和属性值
String attrName=attr.getName();
String attrValue=attr.getValue();
System.out.println("属性名:"+attrName+"------属性值:"+attrValue);
if(attrName.equals("id"))
{//表明当前所解析出来的为id属性值
bookEntity.setId(attrValue);//将id属性值当中的数据信息保存到所建立的bookEntity实体对象当中去
}
}
//对book标签当中的属性值进行遍历完毕之后开始对book标签对象当中的子节点进行遍历操作
List<Element> bookChilds=book.getChildren();
for(Element child : bookChilds)
{//对当前book标签当中的子节点结合对象当中的数据进行遍历输出操作
System.out.println("结点名:"+child.getName()+"-----结点值:"+child.getValue());
if(child.getName().equals("name"))
{
bookEntity.setName(child.getValue());
}
if(child.getName().equals("author"))
{
bookEntity.setAuthor(child.getValue());
}
if(child.getName().equals("year"))
{
bookEntity.setYear(child.getValue());
}
if(child.getName().equals("price"))
{
bookEntity.setPrice(child.getValue());
}
if(child.getName().equals("language"))
{
bookEntity.setLanguage(child.getValue());
}
}
booksList.add(bookEntity);//将当前所解析完毕的book实例化对象添加到集合对象当中
bookEntity=null;//垃圾回收机制将会自动对当前的book对象进行回收处理操作
System.out.println("==========结束解析第"+(bookList.indexOf(book)+1)+"本书========");
}
}
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();
}
System.out.println("-------一共解析到"+booksList.size()+"本书籍存放到了book集合对象当中----");
System.out.println("集合对象当中的第1本书为:"+booksList.get(0).getName());
System.out.println("集合对象当中的第2本书为:"+booksList.get(1).getName());
}
}
程序的运行结果: