1. DOM 解析(Document Object Model):
DOM 解析是一种将整个 XML 文档加载到内存中的方式,以便通过对象模型进行访问和操作。
示例代码:
import org.w3c.dom.Document;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
public class DomParserExample {
public static void main(String[] args) {
try {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
DocumentBuilder builder = factory.newDocumentBuilder();
Document document = builder.parse("example.xml");
// 获取根元素
Element root = document.getDocumentElement();
// 获取子元素列表
NodeList nodeList = root.getElementsByTagName("book");
// 遍历子元素
for (int i = 0; i < nodeList.getLength(); i++) {
Element book = (Element) nodeList.item(i);
String title = book.getElementsByTagName("title").item(0).getTextContent();
String author = book.getElementsByTagName("author").item(0).getTextContent();
System.out.println("Title: " + title + ", Author: " + author);
}
} catch (Exception e) {
e.printStackTrace();
}
}
}
2. SAX 解析(Simple API for XML):
SAX 解析是一种基于事件的解析方式,逐行读取 XML 文档并触发事件来处理数据。
示例代码:
import org.xml.sax.Attributes;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;
import java.io.File;
public class SaxParserExample {
public static void main(String[] args) {
try {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
DefaultHandler handler = new DefaultHandler() {
boolean title = false;
boolean author = false;
@Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if (qName.equalsIgnoreCase("title")) {
title = true;
} else if (qName.equalsIgnoreCase("author")) {
author = true;
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (title) {
System.out.println("Title: " + new String(ch, start, length));
title = false;
} else if (author) {
System.out.println("Author: " + new String(ch, start, length));
author = false;
}
}
};
saxParser.parse(new File("example.xml"), handler);
} catch (Exception e) {
e.printStackTrace();
}
}
}
3. JAXB(Java Architecture for XML Binding):
JAXB 提供了一种将 Java 对象映射到 XML 数据的方式,以及将 XML 数据映射回 Java 对象的方式。
示例代码:
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import java.io.File;
public class JaxbExample {
public static void main(String[] args) {
try {
JAXBContext context = JAXBContext.newInstance(Bookstore.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
Bookstore bookstore = (Bookstore) unmarshaller.unmarshal(new File("example.xml"));
for (Book book : bookstore.getBooks()) {
System.out.println("Title: " + book.getTitle() + ", Author: " + book.getAuthor());
}
} catch (JAXBException e) {
e.printStackTrace();
}
}
}
这是一个简单的 Java XML 教程,涵盖了常见的 DOM、SAX 和 JAXB 解析方法。实际项目中,选择合适的解析方式取决于项目的需求和复杂性。
转载请注明出处:http://www.zyzy.cn/article/detail/450/Java