SAX从上向下解析,一行一行解析。节省内存,不适合CRUD。
XML文档:
<?xml version="1.0" encoding="UTF-8"?>
<书架>
<书>
<书名 name="xxxxx">Java高级</书名>
<作者>武陟县</作者>
<价格>200元</价格>
</书>
<书>
<书名>Java中级</书名>
<作者>打火机</作者>
<价格>2000元</价格>
</书>
</书架>
--------------------------------------------解析所有的内容:-----------------
package XMLParse; import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory; import org.apache.commons.lang.Validate;
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 { public static void main(String[] args) throws Exception {
// 1.创建解析工厂
SAXParserFactory factory = SAXParserFactory.newInstance();
// 2.得到解析器
SAXParser sParser = factory.newSAXParser();
// 3.得到读取器
XMLReader reader = sParser.getXMLReader();
// 4.设置内容处理器
reader.setContentHandler(new ListHandler());
// 5.读取xml文件
reader.parse("src/Book.xml");
}
} // 得到xml文档的所有内容
class ListHandler implements ContentHandler { // 处理开始标签
@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++) {
String attName = atts.getQName(i);
String attValue = atts.getValue(i);
System.out.println(attName+"="+attValue);
}
} // 处理内容
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
// TODO Auto-generated method stub
System.out.println(new String(ch, start, length));
} // 处理结束标签
@Override
public void endElement(String uri, String localName, String qName) throws SAXException {
System.out.println("</" + qName + ">"); } @Override
public void setDocumentLocator(Locator locator) {
// TODO Auto-generated method stub } @Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub } @Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub } @Override
public void startPrefixMapping(String prefix, String uri) throws SAXException {
// TODO Auto-generated method stub } @Override
public void endPrefixMapping(String prefix) throws SAXException {
// TODO Auto-generated method stub } @Override
public void ignorableWhitespace(char[] ch, int start, int length) throws SAXException {
// TODO Auto-generated method stub } @Override
public void processingInstruction(String target, String data) throws SAXException {
// TODO Auto-generated method stub } @Override
public void skippedEntity(String name) throws SAXException {
// TODO Auto-generated method stub } }
结果:
<书架> <书> <书名>
name=xxxxx
Java高级
</书名> <作者>
武陟县
</作者> <价格>
200元
</价格> </书> <书> <书名>
Java中级
</书名> <作者>
打火机
</作者> <价格>
2000元
</价格> </书> </书架>
-----------------------------------------解析指定的标签------------------------------------------------------
package XMLParse; import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory; import org.apache.commons.lang.Validate;
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 { public static void main(String[] args) throws Exception {
// 1.创建解析工厂
SAXParserFactory factory = SAXParserFactory.newInstance();
// 2.得到解析器
SAXParser sParser = factory.newSAXParser();
// 3.得到读取器
XMLReader reader = sParser.getXMLReader();
// 4.设置内容处理器
reader.setContentHandler(new TagValueHandler_1());
// 5.读取xml文件
reader.parse("src/Book.xml");
}
} //读取指定的标签(读取书名)
class TagValueHandler_1 extends DefaultHandler{ private String currentTag; //记住当前的标签是那个
private int needNum = 2; //记住需要第几个
private int currentNum; //记住当前是第几个 @Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
if("书名".equals(qName)){
currentTag="书名";
currentNum++;
}
} @Override
public void endElement(String uri, String localName, String qName) throws SAXException {
currentTag=null;
} @Override
public void characters(char[] ch, int start, int length) throws SAXException {
if("书名".equals(currentTag)&&needNum==currentNum){
System.out.println(new String(ch,start,length));
}
} }
结果:
Java中级
-------------------------------------解析后封装到对象------------------------------
package XMLParse; 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.apache.commons.lang.Validate;
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 Demo4 { public static void main(String[] args) throws Exception {
// 1.创建解析工厂
SAXParserFactory factory = SAXParserFactory.newInstance();
// 2.得到解析器
SAXParser sParser = factory.newSAXParser();
// 3.得到读取器
XMLReader reader = sParser.getXMLReader();
// 4.设置内容处理器
ListBeanHandler listBeanHandler = new ListBeanHandler();
reader.setContentHandler(listBeanHandler);
// 5.读取xml文件
reader.parse("src/Book.xml");
List<Book> list = listBeanHandler.getList();
System.out.println(list); }
} // 读取指定的标签(读取书名)
class ListBeanHandler extends DefaultHandler { private String currentTag; // 记住当前的标签是那个
private List<Book> list = new ArrayList<Book>();
private Book book; @Override
public void startElement(String uri, String localName, String qName, Attributes attributes) throws SAXException {
currentTag = qName;
if ("书".equals(qName)) {
book = new Book();
}
} @Override
public void characters(char[] ch, int start, int length) throws SAXException {
if("书名".equals(currentTag)){
book.setName(new String(ch, start, length));
}
if("作者".equals(currentTag)){
book.setAuthor(new String(ch, start, length));
}
if("价格".equals(currentTag)){
book.setPrice(new String(ch, start, length));
}
} @Override
public void endElement(String uri, String localName, String qName) throws SAXException {
currentTag = null;
if("书".equals(qName)){
list.add(book);
book=null;
}
} public List<Book> getList() {
return list;
} public void setList(List<Book> list) {
this.list = list;
} }
结果:
aaarticlea/png;base64,iVBORw0KGgoAAAANSUhEUgAAA1wAAAAZCAIAAACq+rjRAAAG0UlEQVR4nO2dy7HrKBCGScsOSOlYIUwUWk0Ik4FWs5kQZstdSJahX7z0QPb/1a1bxzRI0DS/GuE6x/373//bv+nvfzzojGlwzjk3TKffeX49xNtq5Zexuujxmpuu0Tim+fXYrjANjd05m/bhF9/u9Xp04KM1djaqvLCL9/rxSTHdKQJoZhruGIlgBxySQqBwm6SwF+bX451bQE8trtvo9At8AgDoACSFAAAAA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