<span style="font-size:14px;"><?xml version="1.0" encoding="utf-8"?> <书架> <书> <书名>java就业培训教程</书名> <作者>郝斌</作者> <售价>36.2元</售价> </书> <书> <书名 name="xxx">javascript就业培训教程</书名> <作者>哈哈哈</作者> <售价>36.2元</售价> </书> </书架> </span>
<span style="font-size:14px;">package com.rj.sax; public class Book { private String name; private String author; private String price; public String getName() { return name; } public void setName(String name) { this.name = name; } public String getAuthor() { return author; } public void setAuthor(String author) { this.author = author; } public String getPrice() { return price; } public void setPrice(String price) { this.price = price; } } </span>
<span style="font-size:14px;">package com.rj.sax; import java.io.IOException; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.ContentHandler; import org.xml.sax.Locator; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; public class Demo1 { /** * sax 解析xml文档 * @throws SAXException * @throws ParserConfigurationException * @throws IOException */ public static void main(String[] args) throws ParserConfigurationException,SAXException,IOException { //1.创建解析工厂 SAXParserFactory factory = SAXParserFactory.newInstance(); //2.得到解析器 SAXParser sp = factory.newSAXParser(); //3.得到读取器 XMLReader reader = sp.getXMLReader(); //4.设置内容处理 reader.setContentHandler(new ListHandler()); //5.读取xml文档内容 reader.parse("src/book.xml"); } } //得到xml文档所有内容 class ListHandler implements ContentHandler{ //ContentHandler-->内容处理器 @Override public void setDocumentLocator(Locator locator) { } @Override public void startDocument() throws SAXException { } @Override public void endDocument() throws SAXException { } @Override public void startPrefixMapping(String prefix,String uri) throws SAXException { } @Override public void endPrefixMapping(String prefix) throws SAXException { } //查找到元素会调用这个方法 @Override public void startElement(String uri,String localName,String qName,Attributes atts) throws SAXException { System.out.println("<"+qName+">"); for(int i=0;atts!=null && i<atts.getLength();i++){ //atts!=null && i<atts.getLength();以后写代码的时候尽量这样写,不要写if(atts!=null) String name = atts.getQName(i); String value = atts.getValue(i); System.out.println(name+"="+value); } } @Override public void endElement(String uri,String qName) throws SAXException { System.out.println("<"+qName+"/>"); } //内容处理的方法 @Override public void characters(char[] ch,int start,int length) throws SAXException { System.out.println(new String(ch,start,length)); } @Override public void ignorableWhitespace(char[] ch,int length) throws SAXException { } @Override public void processingInstruction(String target,String data) throws SAXException { } @Override public void skippedEntity(String name) throws SAXException { } } </span>
<span style="font-size:14px;">package com.rj.sax; import java.io.IOException; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.ContentHandler; import org.xml.sax.Locator; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; public class Demo2 { /** * sax 解析xml文档 * @throws SAXException * @throws ParserConfigurationException * @throws IOException */ public static void main(String[] args) throws ParserConfigurationException,IOException { //1.创建解析工厂 SAXParserFactory factory = SAXParserFactory.newInstance(); //2.得到解析器 SAXParser sp = factory.newSAXParser(); //3.得到读取器 XMLReader reader = sp.getXMLReader(); //4.设置内容处理 reader.setContentHandler(new TagValueHandler()); //5.读取xml文档内容 reader.parse("src/book.xml"); } } //ContentHandler -->处理器 //DefaultHandler是ContentHandler接口的实现类 //获得指定标签的值 -->第二本书 <作者>张孝祥</作者> class TagValueHandler extends DefaultHandler{ private String currentTag;//记录当前解析到的是什么标签 private int needNumber = 2;//记住想获取第几个作者的标签 private int currentNumber; //当前解析到的是第几个 @Override public void startElement(String uri,Attributes attributes) throws SAXException { currentTag = qName; if(currentTag.equals("作者")){ currentNumber++; } } @Override public void characters(char[] ch,int length) throws SAXException { if("作者".equals(currentTag) && currentNumber==needNumber){ System.out.println(new String(ch,length)); } } @Override public void endElement(String uri,String qName) throws SAXException { currentTag = null; } } </span>
<span style="font-size:14px;">package com.rj.sax; import java.io.IOException; import java.util.ArrayList; import java.util.List; import javax.xml.parsers.ParserConfigurationException; import javax.xml.parsers.SAXParser; import javax.xml.parsers.SAXParserFactory; import org.xml.sax.Attributes; import org.xml.sax.ContentHandler; import org.xml.sax.Locator; import org.xml.sax.SAXException; import org.xml.sax.XMLReader; import org.xml.sax.helpers.DefaultHandler; public class Demo3 { /** * sax 解析xml文档 * @throws SAXException * @throws ParserConfigurationException * @throws IOException */ public static void main(String[] args) throws ParserConfigurationException,IOException { //1.创建解析工厂 SAXParserFactory factory = SAXParserFactory.newInstance(); //2.得到解析器 SAXParser sp = factory.newSAXParser(); //3.得到读取器 XMLReader reader = sp.getXMLReader(); //4.设置内容处理 BeanListhandler handler = new BeanListhandler(); reader.setContentHandler(handler); //5.读取xml文档内容 reader.parse("src/book.xml"); List<Book> list = handler.getList(); System.out.println(list); } } //ContentHandler -->处理器 //DefaultHandler是ContentHandler接口的实现类 class BeanListhandler extends DefaultHandler{ private List list = new ArrayList(); private String currentTag ; private Book book; @Override public void startElement(String uri,Attributes attributes) throws SAXException { currentTag = qName; if("书名".equals(currentTag)){ book = new Book(); } } @Override public void characters(char[] ch,int length) throws SAXException { if("书名".equals(currentTag)){ String name = new String(ch,length); book.setName(name); } if("作者".equals(currentTag)){ String author = new String(ch,length); book.setAuthor(author); } if("售价".equals(currentTag)){ String price = new String(ch,length); book.setPrice(price); } } @Override public void endElement(String uri,String qName) throws SAXException { //当遇到每一本书的结束标签的时候,说明一本书已经存贮完毕,要存到List集合中,等待下一本书的存贮 if("书".equals(qName)){ list.add(book); } currentTag = null; } //提供一个List的集合,让外界使用 public List<Book> getList() { return list; } } </span>原文链接:https://www.f2er.com/xml/295363.html