在 Java 中,解析 XML 文件的三种主要方式是 DOM(Document Object Model),SAX(Simple API for XML),以及 StAX(Streaming API for XML)。这些方法各自适用于不同的场景和需求。下面将详细介绍每种方式的步骤流程,并提供相应的示例代码和依赖坐标。
DOM 解析将整个 XML 文档加载到内存中,构建一个树形结构,以便随机访问和操作 XML 文档的各个部分。
导入相关包:
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
获取 DocumentBuilder 实例:
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
解析 XML 文件并获取 Document 对象:
Document document = builder.parse("file.xml");
从 Document 对象中获取元素:
NodeList nodeList = document.getElementsByTagName("elementName");
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.DocumentBuilder;
import org.w3c.dom.Document;
import org.w3c.dom.NodeList;
import org.w3c.dom.Node;
public class DOMParserExample {
public static void main(String[] args) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse("data.xml");
NodeList nodeList = document.getElementsByTagName("book");
for (int i = 0; i < nodeList.getLength(); i++) {
Node node = nodeList.item(i);
if (node.getNodeType() == Node.ELEMENT_NODE) {
System.out.println(node.getNodeName() + ": " + node.getTextContent());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
<dependency>
<groupId>javax.xml</groupId>
<artifactId>jaxp-api</artifactId>
<version>1.4.2</version>
</dependency>
implementation 'javax.xml:jaxp-api:1.4.2'
SAX 解析采用事件驱动模式,逐行解析 XML 文档,无需将整个文档加载到内存中。
导入相关包:
import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
创建并配置自定义的 Handler 类继承自 DefaultHandler:
public class MyHandler extends DefaultHandler {
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) {
// 处理元素开始标签
}
@Override
public void characters(char[] ch, int start, int length) {
// 处理元素文本内容
}
@Override
public void endElement(String uri, String localName, String qName) {
// 处理元素结束标签
}
}
获取 SAXParser 实例并解析 XML:
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
parser.parse("file.xml", new MyHandler());
import org.xml.sax.Attributes;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
public class SAXParserExample {
public static void main(String[] args) {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
parser.parse("data.xml", new MyHandler());
} catch (Exception e) {
e.printStackTrace();
}
}
}
class MyHandler extends DefaultHandler {
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) {
// 处理元素开始标签
}
@Override
public void characters(char[] ch, int start, int length) {
// 处理元素文本内容
}
@Override
public void endElement(String uri, String localName, String qName) {
// 处理元素结束标签
}
}
<dependency>
<groupId>org.xml.sax</groupId>
<artifactId>org.xml.sax</artifactId>
<version>1.2.2</version>
</dependency>
implementation 'org.xml.sax:org.xml.sax:1.2.2'
StAX 解析也是一种事件驱动的方式,类似于 SAX,但提供了更方便的编程模型,允许你在解析过程中操作 XML 内容。
导入相关包:
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
获取 XMLStreamReader 实例并解析 XML:
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader reader = factory.createXMLStreamReader(new FileInputStream("file.xml"));
while (reader.hasNext()) {
int eventType = reader.next();
// 处理事件类型
}
在事件循环中处理不同类型的事件(如元素开始、文本、元素结束等):
while (reader.hasNext()) {
int eventType = reader.next();
if (eventType == XMLStreamReader.START_ELEMENT) {
// 处理元素开始事件
} else if (eventType == XMLStreamReader.CHARACTERS) {
// 处理文本事件
} else if (eventType == XMLStreamReader.END_ELEMENT) {
// 处理元素结束事件
}
}
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamReader;
import java.io.FileInputStream;
public class StAXParserExample {
public static void main(String[] args) {
try {
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader reader = factory.createXMLStreamReader(new FileInputStream("data.xml"));
while (reader.hasNext()) {
int eventType = reader.next();
if (eventType == XMLStreamReader.START_ELEMENT) {
System.out.println("Start Element: " + reader.getLocalName());
} else if (eventType == XMLStreamReader.CHARACTERS) {
System.out.println("Text: " + reader.getText());
} else if (eventType == XMLStreamReader.END_ELEMENT) {
System.out.println("End Element: " + reader.getLocalName());
}
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
<dependency>
<groupId>javax.xml.stream</groupId>
<artifactId>stax-api</artifactId>
<version>1.0-2</version>
</dependency>
implementation 'javax.xml.stream:stax-api:1.0-2'
以上就是解析 XML 文件的三种方式,每种方式都有其适用的场景和优劣势。选择合适的解析方式取决于你的项目需求和性能要求。