xml是一种常见的文本格式,可以为我们提供各系统件信息传输的标准,以及信息持久化存储的模式。通常在接口设计上,报文格式往往就是json和xml两种。json来说更轻量一点,解析也相对容易。
然而现在是xml的时间。。。
java中解析xml方式常见有4种
官方提供的两种
1. DOM
2. SAX
扩展方式2种
1. JDOM
2. DOM4J
DOM是 W3C 组织推荐的处理 XML 的一种方式,JDOM和DOM4J这两种和DOM属于同一种模式都是把xml文件一次读入内存,将其作为一个树这种层次来解析。因为一次性读入内存,导致大文件读取性能上会弱一点。
SAX与DOM方法不同,是一种基于事件的读取模式。读到相应的节点,触发相应的事件。不需要像DOM一样,一开始读入内存中。性能上会优势些。但是这种事件模式上,在文件内容比较复杂的xml文件读取上就会显得比较麻烦。各个节点相互的关系,不容易关联,操作上会没有DOM方法灵活,代码可读性会很糟糕。
JDOM使用上相对简单点,提供一些collection的集合框架,但是性能上比DOM方式弱一点。重在简单。
DOM4J在性能上优于DOM和JDOM, DOM是XML的标准基础,DOM4J性能上又最好(除了SAX),JDOM的话使用上会更方便一点(其实我觉得都差不多),但是性能最弱。SAX这种在xml文件复杂的情况下编程模式上会没有DOM这些便利。
下面代码记录,代码上都比较简单,都是简单读取节点,获取节点属性,以及子节点中的值信息。
xml文件 Books.xml
<?xml version="1.0" encoding="UTF-8"?>
<books>
<book id="1">
<name>think in java</name>
<author>tom</author>
<date>2018</date>
</book>
<book id ="2" att="hhh">
<name>html design</name>
<author>jack</author>
<price>100</price>
</book>
</books>
实体类
package com.yangs.xml.bean;
/**
* Created by Ytadpole on 2018/2/19.
*/
public class Book {
private String id;
private String att;
private String name;
private String author;
private String date;
private String price;
getter()...
setter()...
@Override
public String toString() {
return "Book{" +
"id='" + id + '\'' +
", att='" + att + '\'' +
", name='" + name + '\'' +
", author='" + author + '\'' +
", date='" + date + '\'' +
", price='" + price + '\'' +
'}';
}
}
DOM
package com.yangs.xml.dom;
import com.yangs.xml.bean.Book;
import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Ytadpole on 2018/2/19.
*/
public class DOMTest2 {
public static void main(String[] args) {
List<Book> list = new ArrayList<>();
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
try {
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse("src/com/yangs/xml/Books.xml");
NodeList books = document.getElementsByTagName("book");
for(int i = 0; i < books.getLength(); i++){
//某个书
Book book = new Book();
list.add(book);
Node itemBook = books.item(i);
//属性
NamedNodeMap attrs = itemBook.getAttributes();
for(int j = 0; j < attrs.getLength(); j++){
//某个属性
Node attr = attrs.item(j);
if("id".equals(attr.getNodeName())){
book.setId(attr.getNodeValue());
}else if("att".equals(attr.getNodeName())){
book.setAtt(attr.getNodeValue());
}
}
//子节点
NodeList children = itemBook.getChildNodes();
for(int j = 0; j < children.getLength(); j++){
//某个节点
Node child = children.item(j);
if(child.getNodeType() == Node.ELEMENT_NODE) {//过滤空格,换行这些人为添加的 有助于展示查看的东西
if ("name".equals(child.getNodeName())){
book.setName(child.getTextContent());
}else if ("author".equals(child.getNodeName())){
book.setAuthor(child.getTextContent());
}else if ("price".equals(child.getNodeName())){
book.setPrice(child.getTextContent());
}else if ("date".equals(child.getNodeName())){
book.setDate(child.getTextContent());
}
}
}
}
//展示
for(Book b : list){
System.out.println(b.toString());
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
SAX
处理事件类
package com.yangs.xml.sax;
import com.sun.org.apache.xml.internal.resolver.readers.SAXParserHandler;
import com.yangs.xml.bean.Book;
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Ytadpole on 2018/2/19.
*/
public class MySAXHandler extends SAXParserHandler{
//private int bookIndex;//指向第几本书
private Book book;
private List<Book> list = new ArrayList<>();
private String tmpVlaue;//用于记录book每一个属性的值
public List<Book> getList() {
return list;
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
super.characters(ch, start, length);
tmpVlaue = new String(ch, start, length);
}
@Override
public void endElement(String namespaceURI, String localName, String qName) throws SAXException {
super.endElement(namespaceURI, localName, qName);
if("book".equals(qName)){
book = null;//一次book信息遍历完成吗,清缓存
}
//设置参数
else if("name".equals(qName)){
book.setName(tmpVlaue);
}else if("author".equals(qName)){
book.setAuthor(tmpVlaue);
}else if("date".equals(qName)){
book.setDate(tmpVlaue);
}else if("price".equals(qName)){
book.setPrice(tmpVlaue);
}else{
//System.out.println("bad");
}
}
@Override
public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException {
super.startElement(namespaceURI, localName, qName, atts);
if("book".equals(qName)){
//bookIndex++;
//添加新书
book = new Book();
list.add(book);
//book标签上的属性
for(int i =0; i < atts.getLength(); i++){
String attrName = atts.getQName(i);
if("id".equals(attrName)){
book.setId(atts.getValue(i));
}else if("att".equals(attrName)){
book.setAtt(atts.getValue(i));
}else{
System.out.println("非法的属性"+ atts.getQName(i));
}
}
}
}
@Override
public void startDocument() throws SAXException {
super.startDocument();
System.out.println("解析开始-----");
}
@Override
public void endDocument() throws SAXException {
super.endDocument();
System.out.println("解析结束-----");
}
}
主逻辑
package com.yangs.xml.sax;
import com.yangs.xml.bean.Book;
import org.xml.sax.SAXException;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;
import java.util.List;
/**
* Created by Ytadpole on 2018/2/19.
*/
public class SAXTest {
public static void main(String[] args) {
SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
try {
SAXParser saxParser = saxParserFactory.newSAXParser();
MySAXHandler handler = new MySAXHandler();
saxParser.parse("src/com/yangs/xml/Books.xml", handler);
List<Book> list = handler.getList();
for(Book book : list){
System.out.println(book.toString());
}
} catch (ParserConfigurationException e) {
e.printStackTrace();
} catch (SAXException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
JDOM
package com.yangs.xml.jdom;
import com.yangs.xml.bean.Book;
import org.jdom2.Attribute;
import org.jdom2.Document;
import org.jdom2.Element;
import org.jdom2.JDOMException;
import org.jdom2.input.SAXBuilder;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;
/**
* Created by Ytadpole on 2018/2/19.
*/
public class JDOMTest {
public static void main(String[] args) {
List<Book> list = new ArrayList<>();//存储查询到的对象
try {
InputStream inputStream = new FileInputStream("src/com/yangs/xml/Books.xml");
SAXBuilder saxBuilder = new SAXBuilder();
Document document = saxBuilder.build(inputStream);
Element root = document.getRootElement();
List<Element> books = root.getChildren("book");
for(Element b : books){
//新book
Book book = new Book();
list.add(book);
//属性
List<Attribute> attributes = b.getAttributes();
for(Attribute a : attributes){
if("id".equals(a.getName())){
book.setId(a.getValue());
}else if("att".equals(a.getValue())){
book.setAtt(a.getValue());
}
}
//子节点
List<Element> elements = b.getChildren();
for(Element e : elements){
if("name".equals(e.getName())) {
book.setName(e.getValue());
}else if("author".equals(e.getName())) {
book.setAuthor(e.getValue());
}else if("date".equals(e.getName())) {
book.setDate(e.getValue());
}else if("price".equals(e.getName())) {
book.setPrice(e.getValue());
}
}
}
//输出结果
for(Book b : list){
System.out.println(b.toString());
}
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (JDOMException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}
DOM4J
package com.yangs.xml.dom4j;
import com.yangs.xml.bean.Book;
import org.dom4j.Attribute;
import org.dom4j.Document;
import org.dom4j.DocumentException;
import org.dom4j.Element;
import org.dom4j.io.SAXReader;
import java.io.File;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
/**
* Created by Ytadpole on 2018/2/19.
*/
public class DOM4JTest {
public static void main(String[] args) {
List<Book> list = new ArrayList<>();
SAXReader saxReader = new SAXReader();
try {
//加载文件
Document document = saxReader.read(new File("src/com/yangs/xml/Books.xml"));
//root节点
Element root = (Element) document.getRootElement();
Iterator books = root.elementIterator();
while (books.hasNext()){
//新书
Book book = new Book();
list.add(book);
Element b = (Element) books.next();
//属性
Iterator<Attribute> attrs = b.attributeIterator();
while(attrs.hasNext()){
//某个属性
Attribute attr = attrs.next();
if("id".equals(attr.getName())){
book.setId(attr.getValue());
}else if("att".equals(attr.getName())){
book.setAtt(attr.getValue());
}
}
//子节点
Iterator children = b.elementIterator();
while(children.hasNext()){
//某个子节点
Element child = (Element) children.next();
if("name".equals(child.getName())){
book.setName(child.getStringValue());
}else if("author".equals(child.getName())){
book.setAuthor(child.getStringValue());
}else if("date".equals(child.getName())){
book.setDate(child.getStringValue());
}else if("price".equals(child.getName())){
book.setPrice(child.getStringValue());
}
}
}
//展示
for(Book b : list){
System.out.println(b.toString());
}
} catch (DocumentException e) {
e.printStackTrace();
}
}
}