标签:abc key ext amr 工厂 xmlns clear 节点 函数实现
SAX是一个用于处理XML事件驱动的“推”模型,虽然它不是W3C标准,但它却是一个得到了广泛认可的API。本文主要介绍Java SAX方式处理XML,文中所使用到的软件版本:Java 1.8.0_191。
SAX的全称是Simple APIs for XML,也即XML简单应用程序接口。与DOM不同,SAX提供的访问模式是一种顺序模式,这是一种快速读写XML数据的方式。当使用SAX分析器对XML文档进行分析时,会触发一系列事件,并激活相应的事件处理函数,应用程序通过这些事件处理函数实现对XML文档的访问,因而SAX接口也被称作事件驱动接口。
1、采用事件驱动模式,对内存耗费比较小。
2、适用于只处理XML文件中的数据。
1、编码比较麻烦。
2、很难同时访问XML文件中的多处不同数据。
<?xml version="1.0" encoding="utf-8" ?> <school:class xmlns:school="http://www.w3.org/TR/html4/school/"> <school:student rollno="1" school:testattr="test"> <school:firstname>cxx1</school:firstname> <lastname>Bob1</lastname> <nickname>stars1</nickname> <marks>85</marks> </school:student> <student rollno="2"> <firstname>cxx2</firstname> <lastname>Bob2</lastname> <nickname>stars2</nickname> <marks>85</marks> </student> <student rollno="3"> <firstname>cxx3</firstname> <lastname>Bob3</lastname> <nickname>stars3</nickname> <marks>85</marks> </student> </school:class>
该例子演示如何使用SAX方式解析XML以及使用SAX方式来生成XML。
package com.abc.demo.general.xml; import org.junit.Test; import org.slf4j.Logger; import org.slf4j.LoggerFactory; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.AttributesImpl; import org.xml.sax.helpers.DefaultHandler; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import javax.xml.transform.OutputKeys; import javax.xml.transform.Transformer; import javax.xml.transform.sax.SAXTransformerFactory; import javax.xml.transform.sax.TransformerHandler; import javax.xml.transform.stream.StreamResult; import java.io.ByteArrayOutputStream; public class SaxCase { private static Logger logger = LoggerFactory.getLogger(DomCase.class); /** * 解析xml * @throws Exception */ @Test public void parse() throws Exception { SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); SaxHandler handler = new SaxHandler(logger); parser.parse(DomCase.class.getResourceAsStream("student.xml"), handler); } /** * 生成xml * @throws Exception */ @Test public void toXml() throws Exception { //创建SAX转换工厂 SAXTransformerFactory factory = (SAXTransformerFactory) SAXTransformerFactory.newInstance(); TransformerHandler handler = factory.newTransformerHandler(); //创建handler转换器 Transformer transformer = handler.getTransformer(); //换行 transformer.setOutputProperty(OutputKeys.INDENT, "yes"); transformer.setOutputProperty(OutputKeys.ENCODING, "utf-8"); ByteArrayOutputStream out = new ByteArrayOutputStream(); StreamResult streamResult = new StreamResult(out); handler.setResult(streamResult); AttributesImpl attributes = new AttributesImpl(); //打开doc对象 handler.startDocument(); handler.startElement("http://www.w3.org/TR/html4/school/", "", "school:class", null); //第一个学生 attributes.clear(); attributes.addAttribute("", "", "rollno", "", "1"); attributes.addAttribute("", "", "school:testattr", "", "test"); handler.startElement("", "", "student", attributes); handler.startElement("", "", "school:firstname", null); handler.characters("cxx1".toCharArray(), 0, 4); handler.endElement("", "", "school:firstname"); handler.startElement("", "", "lastname", null); handler.characters("Bob1".toCharArray(), 0, 4); handler.endElement("", "", "lastname"); handler.startElement("", "", "nickname", null); handler.characters("stars1".toCharArray(), 0, 6); handler.endElement("", "", "nickname"); handler.startElement("", "", "marks", null); handler.characters("85".toCharArray(), 0, 2); handler.endElement("", "", "marks"); handler.endElement("", "", "student"); //第二个学生 attributes.clear(); attributes.addAttribute("", "", "rollno", "", "2"); handler.startElement("", "", "student", attributes); handler.startElement("", "", "firstname", null); handler.characters("cxx2".toCharArray(), 0, 4); handler.endElement("", "", "firstname"); handler.startElement("", "", "lastname", null); handler.characters("Bob2".toCharArray(), 0, 4); handler.endElement("", "", "lastname"); handler.startElement("", "", "nickname", null); handler.characters("stars2".toCharArray(), 0, 6); handler.endElement("", "", "nickname"); handler.startElement("", "", "marks", null); handler.characters("85".toCharArray(), 0, 2); handler.endElement("", "", "marks"); handler.endElement("", "", "student"); //第三个学生 attributes.clear(); attributes.addAttribute("", "", "rollno", "", "3"); handler.startElement("", "", "student", attributes); handler.startElement("", "", "firstname", null); handler.characters("cxx3".toCharArray(), 0, 4); handler.endElement("", "", "firstname"); handler.startElement("", "", "lastname", null); handler.characters("Bob3".toCharArray(), 0, 4); handler.endElement("", "", "lastname"); handler.startElement("", "", "nickname", null); handler.characters("stars3".toCharArray(), 0, 6); handler.endElement("", "", "nickname"); handler.startElement("", "", "marks", null); handler.characters("85".toCharArray(), 0, 2); handler.endElement("", "", "marks"); handler.endElement("", "", "student"); handler.endElement("", "", "school:class"); handler.endDocument(); logger.info(out.toString()); } } class SaxHandler extends DefaultHandler { private String value; private Logger logger; public SaxHandler(Logger logger) { this.logger = logger; } @Override public void startDocument() throws SAXException { super.startDocument(); logger.info("SAX解析开始"); } @Override public void endDocument() throws SAXException { super.endDocument(); logger.info("SAX解析结束"); } @Override public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { super.startElement(uri, localName, qName, attributes); if (qName.equals("student") || qName.equals("school:student")) { logger.info("学生编号" + attributes.getValue("rollno")); if (attributes.getValue("school:testattr") != null) { logger.info("testattr=" + attributes.getValue("school:testattr")); } } else if (qName.equals("firstname") || qName.equals("lastname") || qName.equals("nickname") || qName.equals("marks")) { //logger.info(qName + "开始"); } } @Override public void endElement(String uri, String localName, String qName) throws SAXException { super.endElement(uri, localName, qName); if (qName.equals("firstname") || qName.equals("school:firstname") || qName.equals("lastname") || qName.equals("nickname") || qName.equals("marks")) { logger.info(qName + ":" + value); } } @Override 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("")) { // logger.info("节点值是:" + value); } } }
标签:abc key ext amr 工厂 xmlns clear 节点 函数实现
原文地址:https://www.cnblogs.com/wuyongyin/p/14268108.html