1、示例XML为hello.xml,如下:

天龙八部

金庸

2014

88

鹿鼎记

2015

66

中文

射雕英雄传

金庸

2016

44

一、DOM(Document Object Model)解析

优点:

1)允许应用程序对数据和结构做出更改。

2)访问是双向的,可以在任何时候再树中上、下导航获取、操作任意部分的数据。

缺点:

解析XML文档的需要加载整个文档来构造层次结构,消耗内存资源大。

应用范围:

遍历能力强,常应用于XML文档需要频繁改变的服务中。

Java使用步骤:

创建一个DocumentBuilderFactory对象。

创建一个DocumentBuilder对象。

通过DocumentBuilder的parse方法加载XML到当前工程目录下。

通过getElementsByTagName方法获取所有XML所有节点的集合。

遍历所有节点。

通过item方法获取某个节点的属性。

通过getNodeName和getNodeValue方法获取属性名和属性值。

通过getChildNodes方法获取子节点,并遍历所有子节点。

通过getNodeName和getTextContent方法获取子节点名称和子节点值。

package Paint;

import java.io.IOException;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;

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;

public class DOMTest {

public static void main(String[] args) {

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

try {

DocumentBuilder db = dbf.newDocumentBuilder();

Document document = db.parse("./src/Paint/hello.xml");

NodeList bookList = document.getElementsByTagName("book");//节点集

int bookCnt = bookList.getLength();

System.err.println("一共获取到" + bookCnt +"本书");

for(int i=0; i

Node book = bookList.item(i);

NamedNodeMap attrs = book.getAttributes();

for(int j=0; j

Node attr = attrs.item(j);

System.err.println(attr.getNodeName()+"---"+attr.getNodeValue());//id

}

NodeList childNodes = book.getChildNodes();

for(int k=0; k

if(childNodes.item(k).getNodeType() == Node.ELEMENT_NODE){

System.out.println(childNodes.item(k).getNodeName()+"---" + childNodes.item(k).getTextContent());

}

}

}

} catch (ParserConfigurationException e) {

e.printStackTrace();

} catch (SAXException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

二. SAX(Simple API for XML)解析

优点:

1)不需要等待所有的数据被处理,解析就可以开始。

2)只在读取数据时检查数据,不需要保存在内存中。

3) 可以在某一个条件满足时停止解析,不必要解析整个文档。

4) 效率和性能较高,能解析大于系统内存的文档。

缺点:

1)解析逻辑复杂,需要应用层自己负责逻辑处理,文档越复杂程序越复杂。

2)单向导航,无法定位文档层次,很难同时同时访问同一文档的不同部分数据,不支持XPath.

解析步骤:

获取一个SAXParserFactory的实例。

通过factory获取SAXParser实例。

创建一个handler对象。

通过parser的parse()方法来解析XML。

private String id;

private String name;

private String author;

private String year;

private String price;

private String language;

// getter和setter方法略

}

package Paint;

import java.io.IOException;

import javax.xml.parsers.ParserConfigurationException;

import javax.xml.parsers.SAXParser;

import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.SAXException;

public class SAXTest {

public static void main(String[] args) {

//获取实例

SAXParserFactory factory = SAXParserFactory.newInstance();

try {

SAXParser parser = factory.newSAXParser();

SAXParserHandler handler = new SAXParserHandler();

parser.parse("./src/Paint/hello.xml", handler);

System.err.println("共有"+ handler.getBookList().size()+ "本书");

for(Book book : handler.getBookList()){

System.out.println(book.getName());

System.out.println("id=" + book.getId());

System.out.println(book.getAuthor());

System.out.println(book.getYear());

System.out.println(book.getPrice());

System.out.println(book.getLanguage());

}

} catch (ParserConfigurationException e) {

e.printStackTrace();

} catch (SAXException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

package Paint;

import java.util.ArrayList;

import org.xml.sax.Attributes;

import org.xml.sax.SAXException;

import org.xml.sax.helpers.DefaultHandler;

public class SAXParserHandler extends DefaultHandler {

String value = null;

Book book = null;

private ArrayList bookList = new ArrayList();

public ArrayList getBookList() {

return bookList;

}

/*

* XML解析开始

*/

public void startDocument() throws SAXException {

super.startDocument();

System.out.println("xml解析开始");

}

/*

* XML解析结束

*/

public void endDocument() throws SAXException {

super.endDocument();

System.out.println("xml解析结束");

}

/*

* 解析XML元素开始

*/

public void startElement(String uri, String localName, String qName,

Attributes attributes) throws SAXException {

super.startElement(uri, localName, qName, attributes);

if(qName.equals("book")){

book = new Book();

for(int i=0; i

System.out.println(attributes.getQName(i)+"---"+attributes.getValue(i));

if(attributes.getQName(i).equals("id")){

book.setId(attributes.getValue(i));

}

}

}else if(!qName.equals("bookstore")){

System.out.print("节点名:"+ qName + "---");

}

}

/*

*解析XML元素 结束

*/

public void endElement(String uri, String localName, String qName)

throws SAXException {

super.endElement(uri, localName, qName);

if(qName.equals("book")){

bookList.add(book);

book = null;

}

else if(qName.equals("name")){

book.setName(value);

}else if(qName.equals("year")){

book.setYear(value);

}else if(qName.equals("author")){

book.setAuthor(value);

}else if(qName.equals("price")){

book.setPrice(value);

}else if(qName.equals("language")){

book.setLanguage(value);

}

}

public void characters(char[] ch, int start, int length)

throws SAXException {

super.characters(ch, start, length);

//获取节点值数组

value = new String(ch, start, length);

if(!value.trim().equals("")){

System.out.println("节点值:"+value);

}

}

}

三、JDOM解析(需要引入jdom.jar包)

特征:

1、仅使用具体类,而不使用接口。

2、API大量使用了Collections类。

步骤:

1.创建一个SAXBuilder的对象

2.创建一个输入流,将xml文件加载到输入流中

3.通过saxBuilder的build方法,将输入流加载到saxBuilder中

4.通过document对象获取xml文件的根节点

5.获取根节点下的子节点的List集合

package Paint;

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 {

private static ArrayList booksList = new ArrayList();

public static void main(String[] args) {

SAXBuilder saxBuilder = new SAXBuilder();

InputStream in;

try {

in = new FileInputStream("./src/Paint/hello.xml");

InputStreamReader isr = new InputStreamReader(in, "UTF-8");

Document document = saxBuilder.build(isr);

Element rootElement = document.getRootElement();

List bookList = rootElement.getChildren();

for (Element book : bookList) {

Book bookEntity = new Book();

List attrList = book.getAttributes();

for (Attribute attr : attrList) {

String attrName = attr.getName();

String attrValue = attr.getValue();

System.out.println( attrName + "----" + attrValue);

if (attrName.equals("id")) {

bookEntity.setId(attrValue);

}

}

// 对book节点的子节点的节点名以及节点值的遍历

List bookChilds = book.getChildren();

for (Element child : bookChilds) {

System.out.println(child.getName() + "----"+ child.getValue());

if (child.getName().equals("name")) {

bookEntity.setName(child.getValue());

}

else if (child.getName().equals("author")) {

bookEntity.setAuthor(child.getValue());

}

else if (child.getName().equals("year")) {

bookEntity.setYear(child.getValue());

}

else if (child.getName().equals("price")) {

bookEntity.setPrice(child.getValue());

}

else if (child.getName().equals("language")) {

bookEntity.setLanguage(child.getValue());

}

}

booksList.add(bookEntity);

bookEntity = null;

System.out.println(booksList.size());

System.out.println(booksList.get(0).getId());

System.out.println(booksList.get(0).getName());

}

} catch (FileNotFoundException e) {

e.printStackTrace();

} catch (JDOMException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

}

}

四、 DOM4J(Document Object Model for Java)解析(需要引入dom4j-1.6.1.jar)

优点:

性能很好

大量使用Java集合类,开发简便,同时也提供了一些提高性能的代替方法。

支持XPath。

缺点:

API比较复杂。

步骤:

1.创建SAXReader的对象reader

2.通过reader对象的read()方法加载books.xml文件,获取document对象

3.通过document对象获取根节点bookstore

4.通过element对象的elementIterator获取迭代器

5.遍历迭代器,获取根节点中的信息

6.获取book的属性名和属性值

7.通过book对象的elementIterator获取节点元素迭代器

8.遍历迭代器,获取子节点中的信息

9.获取节点名和节点值

package Paint;

import java.io.File;

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

import org.dom4j.Attribute;

import org.dom4j.Document;

import org.dom4j.DocumentException;

import org.dom4j.Element;

import org.dom4j.io.SAXReader;

public class DOM4JTest {

public static void main(String[] args) {

ArrayList bookList = new ArrayList();

SAXReader reader = new SAXReader();

try {

Document document = reader.read(new File("./src/Paint/hello.xml"));

Element bookStore = document.getRootElement();

Iterator it = bookStore.elementIterator();

while (it.hasNext()) {

Element book = (Element) it.next();

Book bookData = new Book();

List bookAttrs = book.attributes();

for (Attribute attr : bookAttrs) {

System.out.println(attr.getName() + "---" + attr.getValue());

if(attr.getName().equals("id")){

bookData.setId(attr.getValue());

}

}

Iterator itt = book.elementIterator();

while (itt.hasNext()) {

Element bookChild = (Element) itt.next();

System.out.println(bookChild.getName()+ "---" + bookChild.getText());

if(bookChild.getName().equals("name")){

bookData.setName(bookChild.getText());

}else if(bookChild.getName().equals("author")){

bookData.setAuthor(bookChild.getText());

}else if(bookChild.getName().equals("year")){

bookData.setYear(bookChild.getText());

}else if(bookChild.getName().equals("price")){

bookData.setPrice(bookChild.getText());

}else if(bookChild.getName().equals("language")){

bookData.setLanguage(bookChild.getText());

}

}

//遍历完一个节点,将该节点信息添加到列表中

bookList.add(bookData);

}

} catch (DocumentException e) {

e.printStackTrace();

}

//输出保存在内存中XML信息

for(Book book:bookList){

System.out.println(book.getName());

System.out.println("id=" + book.getId());

System.out.println(book.getAuthor());

System.out.println(book.getYear());

System.out.println(book.getPrice());

System.out.println(book.getLanguage());

}

}

}

Logo

魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。

更多推荐