Java解析XML详解
XML 概述
XML(Extensible Markup Language) 是一种标记语言,用于存储和传输数据。XML 文档由许多元素构成,每个元素包含一个开始标签、一个结束标签和其中间的一些内容。
XML 和 HTML 最大的不同在于,XML 的标签是自定义的,因此具有更强的灵活性和可扩展性。XML 通常用于将数据从一种格式转换为另一种格式,并用于各种应用程序之间的数据交换。
Java 解析 XML
Java 通过许多 API 来解析 XML 文档。本文将介绍两种最常用的 API:
- JAXP(Java API for XML Processing)
JAXP 是 Java 中用于处理 XML 的标准 API,它提供了解析器和文档对象模型(DOM)。
- SAX(Simple API for XML)
SAX 是 Java 中另一种解析 XML 的 API,它是一种基于事件驱动的解析器。相比于 DOM,它更适合处理大型文档,因为它不需要将整个文档加载到内存中。
JAXP 解析 XML
解析 XML 文档步骤
- 获取一个 javax.xml.parsers.DocumentBuilderFactory 的实例。
- 通过 DocumentBuilderFactory 创建一个 javax.xml.parsers.DocumentBuilder 实例。
- 通过 DocumentBuilder 的 parse() 方法解析 XML 文档。
- 通过 DOM 解析 XML 文档并操作元素。
示例
假设有如下 XML 文档:
<catalog>
<book id="bk101">
<author>Gambardella, Matthew</author>
<title>XML Developer's Guide</title>
<genre>Computer</genre>
<price>44.95</price>
<publish_date>2000-10-01</publish_date>
<description>An in-depth look at creating applications
with XML.</description>
</book>
<book id="bk102">
<author>Ralls, Kim</author>
<title>Midnight Rain</title>
<genre>Fantasy</genre>
<price>5.95</price>
<publish_date>2000-12-16</publish_date>
<description>A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.</description>
</book>
</catalog>
使用 JAXP 解析 XML 文档的代码如下:
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import java.io.File;
public class JAXPExample {
public static void main(String[] args) throws Exception {
File xmlFile = new File("example.xml");
DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
Document doc = dBuilder.parse(xmlFile);
doc.getDocumentElement().normalize();
NodeList bookList = doc.getElementsByTagName("book");
for (int i = 0; i < bookList.getLength(); i++) {
Element book = (Element) bookList.item(i);
String id = book.getAttribute("id");
String author = book.getElementsByTagName("author").item(0).getTextContent();
String title = book.getElementsByTagName("title").item(0).getTextContent();
String genre = book.getElementsByTagName("genre").item(0).getTextContent();
double price = Double.parseDouble(book.getElementsByTagName("price").item(0).getTextContent());
String publishDate = book.getElementsByTagName("publish_date").item(0).getTextContent();
String description = book.getElementsByTagName("description").item(0).getTextContent();
System.out.println(String.format("Book %s:%nAuthor: %s%nTitle: %s%nGenre: %s%nPrice: %s%nPublish Date: %s%nDescription: %s%n",
id, author, title, genre, price, publishDate, description));
}
}
}
运行该程序后,结果如下:
Book bk101:
Author: Gambardella, Matthew
Title: XML Developer's Guide
Genre: Computer
Price: 44.95
Publish Date: 2000-10-01
Description: An in-depth look at creating applications
with XML.
Book bk102:
Author: Ralls, Kim
Title: Midnight Rain
Genre: Fantasy
Price: 5.95
Publish Date: 2000-12-16
Description: A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.
SAX 解析 XML
解析 XML 文档步骤
- 创建一个 SAXParserFactory 的实例。
- 通过 SAXParserFactory 创建一个 SAXParser 的实例。
- 创建自己的 SAX Handler 类继承 DefaultHandler。
- 通过 SAXParser 的 parse() 方法解析 XML 文档。
- 在 SAX Handler 类中实现处理 XML 元素的方法。
示例
使用 SAX 解析 XML 文档的代码如下:
import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.SAXException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.IOException;
public class SAXExample extends DefaultHandler {
boolean bAuthor = false;
boolean bTitle = false;
boolean bGenre = false;
boolean bPrice = false;
boolean bPublishDate = false;
boolean bDescription = false;
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equalsIgnoreCase("book")) {
String id = attributes.getValue("id");
System.out.println(String.format("Book %s:", id));
} else if (qName.equalsIgnoreCase("author")) {
bAuthor = true;
} else if (qName.equalsIgnoreCase("title")) {
bTitle = true;
} else if (qName.equalsIgnoreCase("genre")) {
bGenre = true;
} else if (qName.equalsIgnoreCase("price")) {
bPrice = true;
} else if (qName.equalsIgnoreCase("publish_date")) {
bPublishDate = true;
} else if (qName.equalsIgnoreCase("description")) {
bDescription = true;
}
}
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (bAuthor) {
System.out.println(String.format("Author: %s", new String(ch, start, length)));
bAuthor = false;
} else if (bTitle) {
System.out.println(String.format("Title: %s", new String(ch, start, length)));
bTitle = false;
} else if (bGenre) {
System.out.println(String.format("Genre: %s", new String(ch, start, length)));
bGenre = false;
} else if (bPrice) {
System.out.println(String.format("Price: %s", Double.parseDouble(new String(ch, start, length))));
bPrice = false;
} else if (bPublishDate) {
System.out.println(String.format("Publish Date: %s", new String(ch, start, length)));
bPublishDate = false;
} else if (bDescription) {
System.out.println(String.format("Description: %s", new String(ch, start, length)));
bDescription = false;
}
}
public static void main(String[] args) throws Exception {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
SAXExample handler = new SAXExample();
saxParser.parse("example.xml", handler);
}
}
运行该程序后,结果如下:
Book bk101:
Author: Gambardella, Matthew
Title: XML Developer's Guide
Genre: Computer
Price: 44.95
Publish Date: 2000-10-01
Description: An in-depth look at creating applications
with XML.
Book bk102:
Author: Ralls, Kim
Title: Midnight Rain
Genre: Fantasy
Price: 5.95
Publish Date: 2000-12-16
Description: A former architect battles corporate zombies,
an evil sorceress, and her own childhood to become queen
of the world.
本站文章如无特殊说明,均为本站原创,如若转载,请注明出处:java解析XML详解 - Python技术站