- 2023-05-24 11:30:45
- 939 热度
- 0 评论
SAX,全称Simple API for XML,既是指一种接口,也是指一个软件包。SAX最初是由David Megginson采用Java语言开发,之后SAX很快在Java开发者中流行起来。San现在负责管理其原始API的开发工作,这是一种公开的、开放源代码软件。不同于其他大多数XML标准的是,SAX没有语言开发商必须遵守的标准SAX参考版本。因此,SAX的不同实现可能采用区别很大的接口。
作为接口,SAX是事件驱动型XML解析的一个标准接口(standard interface)不会改变,已被OASIS(Organization for the Advancement of Structured Information Standards)所采纳。作为软件包,SAX最早的开发始于1997年12月,由一些在互联网上分散的程序员合作进行。后来,参与开发的程序员越来越多,组成了互联网上的XML-DEV社区。五个月以后,1998年5月,SAX 1.0版由XML-DEV正式发布。目前,最新的版本是SAX 2.0。2.0版本在多处与1.0版本不兼容,包括一些类和方法的名字。
XML文件:
<?xml version="1.0" encoding="utf-8"?> <persons> <person> <name>java無名</name> <sex>man</sex> <age>30</age> </person> <person> <name>崔素强</name> <sex>man</sex> <age>26</age> </person> </persons>
为此建立响应的对象:
package com.test; public class Xml { private String name; private String sex; private int age; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getSex() { return sex; } public void setSex(String sex) { this.sex = sex; } public int getAge() { return age; } public void setAge(int age) { this.age = age; } }
参考的解析代码:
package com.test; import java.io.FileInputStream; import java.io.InputStream; import java.util.ArrayList; import java.util.List; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.SAXException; import org.xml.sax.helpers.DefaultHandler; /** * @说明 SAX解析XML * @author cuisuqiang * @version 1.0 * @since */ public class SaxDemo{ public static void main(String[] args) { try { SaxDemo sd = new SaxDemo(); String file = "C:\\p.xml"; // 文件存放位置 sd.parserXml(file); } catch (Exception e) { e.printStackTrace(); } } public void parserXml(String fileName) throws Exception { SAXParserFactory saxfac = SAXParserFactory.newInstance(); SAXParser saxparser = saxfac.newSAXParser(); InputStream is = new FileInputStream(fileName); SAXHandler sh = new SAXHandler(); saxparser.parse(is, sh); List<Xml> list = sh.getPersons(); System.out.println("====输出解析到的内容===="); for(Xml x : list){ System.out.println(x.getName() + "\t" + x.getSex() + "\t" + x.getAge()); } } } class SAXHandler extends DefaultHandler { private List<Xml> list = null; private Xml xml = null; private String str = null; public List<Xml> getPersons() { return list; } public void startDocument() throws SAXException { list = new ArrayList<Xml>(); System.out.println("XML解析开始"); } public void endDocument() throws SAXException { System.out.println("XML解析结束"); } public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException { if("person".equals(qName)){ xml = new Xml(); System.out.println("开始实体解析"); } } public void endElement(String uri, String localName, String qName) throws SAXException { if("name".equals(qName)){ xml.setName(str); } if("sex".equals(qName)){ xml.setSex(str); } if("age".equals(qName)){ xml.setAge(Integer.parseInt(str)); } if("person".equals(qName)){ list.add(xml); System.out.println("实体解析结束"); } } // XML节点的内容会进入此方法 public void characters(char[] ch, int start, int length) throws SAXException { str = new String(ch, start, length); } }
结束。
0 评论
留下评论
热门标签
- Spring(403)
- Boot(208)
- Spring Boot(187)
- Spring Cloud(82)
- Java(82)
- Cloud(82)
- Security(60)
- Spring Security(54)
- Boot2(51)
- Spring Boot2(51)
- Redis(31)
- SQL(29)
- Mysql(25)
- Dalston(24)
- IDE(24)
- mongoDB(22)
- MVC(22)
- JDBC(22)
- IDEA(22)
- Web(21)
- CLI(20)
- Alibaba(19)
- SpringMVC(19)
- Docker(17)
- SpringBoot(17)
- Git(16)
- Eclipse(16)
- Vue(16)
- JPA(15)
- Apache(15)
- ORA(15)
- Tomcat(14)
- Linux(14)
- HTTP(14)
- Mybatis(14)
- Oracle(14)
- jdk(14)
- OAuth(13)
- Nacos(13)
- Pro(13)
- XML(13)
- JdbcTemplate(13)
- JSON(12)
- OAuth2(12)
- Data(12)
- int(11)
- Myeclipse(11)
- stream(11)
- not(10)
- Bug(10)
- Hystrix(9)
- ast(9)
- maven(9)
- Map(9)
- Swagger(8)
- APP(8)
- Bit(8)
- API(8)
- session(8)
- Window(8)
- windows(7)
- too(7)
- HTML(7)
- Github(7)
- JavaMail(7)
- Cache(7)
- File(7)
- IntelliJ(7)
- mail(7)
- Server(6)
- nginx(6)
- jar(6)
- ueditor(6)
- ehcache(6)
- UDP(6)
- RabbitMQ(6)
- and(6)
- star(6)
- Excel(6)
- Log4J(6)
- pushlet(6)
- apt(6)
- Freemarker(6)
- read(6)
- WebFlux(6)
- JSP(6)
- Bean(6)
- error(6)
- are(5)
- SVN(5)
- for(5)
- DOM(5)
- Sentinel(5)
- the(5)
- JWT(5)
- rdquo(5)
- PHP(5)
- Struts(5)
- string(5)
- script(5)