package com.sax;
import java.io.File;
import java.io.IOException;
import java.util.List;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import org.xml.sax.SAXException;
public class TestBook {
public static void main(String[] args) {
try {
List<Book> l = SAXmethod();
for (Book b : l) {
System.out.print(b.getId() + "\t");
System.out.print(b.getName() + "\t");
System.out.print(b.getAuther() + "\t");
System.out.print(b.getLanguage() + "\t");
System.out.println(b.getMoney() + "\t");
}
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
public static List<Book> SAXmethod() throws ParserConfigurationException, SAXException, IOException {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
DOM4j sax = new DOM4j();
sp.parse(new File("F:\\workspace\\15-10-21\\src\\com\\sax\\Book.xml"), sax);
return sax.getL();
}
}
不错!
不过有注释就更好了