<?xml version="1.0" encoding="utf-8"?>
<books>
<book bookno="001">
<title>java入門</title>
<author>曉波</author>
<price>2121</price>
</book>
<book bookno="002">
<title>java精通</title>
<author>曉波</author>
<price>1314</price>
</book>
<book bookno="003">
<title>java大神</title>
<author>曉波</author>
<price>520</price>
</book>
</books>java
package com.xiaobo.xmlparse;git
public class Book {github
private String bookno;
private String title;
private String author;
private Double price;
public String getBookno() {
return bookno;
}
@Override
public String toString() {
return "Book [bookno=" + bookno + ", title=" + title + ", author=" + author + ", price=" + price + "]";
}
public void setBookno(String bookno) {
this.bookno = bookno;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public Double getPrice() {
return price;
}
public void setPrice(Double price) {
this.price = price;
}
}dom
public interface XmlParse {
public List<Book> parseXml(String filename);
}ide
public class SaxParser implements XmlParse{ui
@Override
public List<Book> parseXml(String filename) {
List<Book> list=new ArrayList<>();
SAXParserFactory sFactory=SAXParserFactory.newInstance();
mySaxHandler handler=new mySaxHandler();
try {
SAXParser parser = sFactory.newSAXParser();
InputStream in=new FileInputStream(filename);
parser.parse(in, handler);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
list=handler.getList();
return list;
}
public static void main(String[] args) {
SaxParser saxParser=new SaxParser();
List<Book> list = saxParser.parseXml("book.xml");
System.out.println("解析完成"+list);
}this
}spa
public class DomParser implements XmlParse{.net
@Override
public List<Book> parseXml(String filename) {
// TODO Auto-generated method stub
List<Book> list=new ArrayList<>();
DocumentBuilderFactory factory=DocumentBuilderFactory.newInstance();
Document document=null;
try {
DocumentBuilder builder = factory.newDocumentBuilder();
document = builder.parse(new File(filename));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
//得到books
Node books = document.getDocumentElement();
//獲取book
NodeList booklist = books.getChildNodes();
//獲取單個的book
for(int i=0;i<booklist.getLength();i++){
//獲取單個book屬性
Node bookele = booklist.item(i);
if(bookele.getNodeName().equals("book")){
Book book=new Book();
//獲取book元素的屬性,是一個map
NamedNodeMap map = bookele.getAttributes();
//獲取指定的屬性,返回一個節點
Node booknoNode = map.getNamedItem("bookno");
//獲取值
String bookno = booknoNode.getNodeValue();
book.setBookno(bookno);
//獲取book元素下面的子節點是個list
NodeList bookchildlist = bookele.getChildNodes();
for(int j=0;j<bookchildlist.getLength();j++){
//獲取指定的元素
Node bookchild= bookchildlist.item(j);
//獲得名字,進行判斷
String bookchildname=bookchild.getNodeName();
switch(bookchildname){
case "title":
//book進行對應的屬性
book.setTitle(bookchild.getTextContent().trim());
break;
case "author":
//book進行對應的屬性
book.setAuthor(bookchild.getTextContent().trim());
break;
case "price":
//book進行對應的屬性
book.setPrice(Double.valueOf(bookchild.getTextContent().trim()));
break;
default:
break;
}
}
list.add(book);
}
}
return list;
}
public static void main(String[] args) {
DomParser parser=new DomParser();
List<Book> list = parser.parseXml("book.xml");
System.out.println("Dom 解析結果爲:");
for(Book book:list){
System.out.println("book is "+book);
}
}
}xml
public class Dom4jParse {
public static void main(String[] args) {
Dom4jParse dParse=new Dom4jParse();
dParse.parseXml();
}
public void parseXml() {
// TODO Auto-generated method stub
//建立SAXReader對象
SAXReader reader=new SAXReader();
//經過reader將xml轉換爲document對象
try {
Document document = reader.read(new File("book.xml"));
//獲取根元素節點
Element books = document.getRootElement();
//遍歷全部的節點
ListNodes(books);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public void ListNodes(Element ele){
System.out.println("當前節點名稱:"+ele.getName());
//能夠利用屬性list進行遍歷books上面的屬性
List<Attribute> list = ele.attributes();
for(Attribute attr:list){
System.out.println(attr.getName()+"----"+attr.getValue());
}
if(!ele.getText().trim().equals("")){
System.out.println("文本內容: "+ele.getText().trim());
}
//利用elementIterator對books節點下面的子節點進行遍歷
Iterator<Element> book = ele.elementIterator();
//在迭代中將整個文檔遍歷
while(book.hasNext()){
Element e= book.next();
ListNodes(e);
}
}
}