问:JDOM如何解析XML文件?
JDOM是一个开源项目,它基于树型结构,利用纯JAVA的技术对XML文档实现解析、生成、序列化以及多种操作
问:使用JDOM需要什么条件?
使用JDOM方式解析XML文档,需要在工程文件中导入jdom.jar包 下载地址
(1)示例代码:
import org.jdom.Document;
import org.jdom.Element;
import org.jdom.input.SAXBuilder;
import org.jdom.output.Format;
import org.jdom.output.XMLOutputter;
import org.jdom.xpath.XPath;
public class JDomResolve {
public static void main(String[] args) throws IOException{
String file = "file/books.xml";
InputStream inputStream = new FileInputStream(file);
JDomResolve jDomResolve = new JDomResolve();
//1.JDOM方式解析xml文件
try {
List<Book> bookList = jDomResolve.getBooks(inputStream);
for(Book book : bookList ){
System.out.println(book.toString());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//2.JDOM方式创建xml文件
jDomResolve.jDomCreateXml();
}
/**
* JDom方式解析xml
*/
public List<Book> books = new ArrayList<Book>();
public Book book = new Book();
public List<Book> getBooks(InputStream inputStream) throws Exception{
//创建SAXBuilder实例
SAXBuilder sb = new SAXBuilder();
//获取document
Document document = sb.build(inputStream);
//获取根节点
Element rootElement = document.getRootElement();
//获取bookList
List listElement = XPath.selectNodes(rootElement, "//bookstore/book");
//循环取值(枚举)
for(Iterator it = listElement.iterator();it.hasNext();){
Element bookElement = (Element) it.next();
String id = bookElement.getAttributeValue("id");
String name = bookElement.getChildText("bookName");
String author = bookElement.getChildText("author");
String year = bookElement.getChildText("year");
book.setId(Integer.parseInt(id));
book.setBookName(name);
book.setAuthor(author);
book.setYear(year);
books.add(book);
book=new Book();
}
return books;
}
/**
* JDom方式创建xml文件
*/
public void jDomCreateXml() throws FileNotFoundException, IOException {
//创建document文档节点
Document document = new Document();
//创建根节点
Element bookstore = new Element("bookstore");
//将bookstore添加到document下
document.addContent(bookstore);
//创建子节点
Element book = new Element("book");
//添加属性
book.setAttribute("id", "1");
//将子节点添加到根节点下
bookstore.addContent(book);
//创建子节点
Element name = new Element("name");
name.setText("冰与火之歌");
Element author = new Element("author");
author.setText("乔治马丁");
Element year = new Element("year");
year.setText("2004");
Element price = new Element("price");
price.setText("89");
//将子节点添加到book下
book.addContent(name);
book.addContent(author);
book.addContent(year);
book.addContent(price);
//将创建的xml文档写到流中
XMLOutputter out = new XMLOutputter();
//设置生成xml文档的格式
Format format = Format.getCompactFormat();
//自定义xml文档的缩进
format.setIndent(" ");
out.setFormat(format);
out.output(document, new FileOutputStream("file/cjdom.xml"));
}
}
(2)Book实体类代码:
public class Book {
private int id; //id-book
private String bookName;//书名
private String author;//作者
private String year;//年份
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getBookName() {
return bookName;
}
public void setBookName(String bookName) {
this.bookName = bookName;
}
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;
}
@Override
public String toString() {
return this.id+"---"+this.bookName+"---"+this.author+"---"+this.year;
}
}
(3)创建的cjdom.xml代码截图: