SAX解析xml步骤
通过SAXParseFactory的静态newInstance()方法获取SAXParserFactory实例factory
通过SAXParserFactory实例的newSAXParser()方法返回SAXParser实例parser
创建一个类继承DefaultHandle,重写方法进行业务处理并创建这个类的实例handle
重写DefaultHandle类的方法
startElement方法用来遍历xml文件的开始标签;
endElement方法用来遍历xml文件的结束标签;
startDocument方法用来标识解析开始;
endDocument方法用来标识解析结束。
characters方法用来获取text
其中:参数qName 遍历到的元素的名称
且同样会出现空白和换行的问题
下面直接上代码
<?xml version="1.0" encoding="UTF-8"?> <bookstore> <book id="1"> <name>JAVA编程思想</name> <anthor>****</anthor> <year>2000</year> </book> <book id="2"> <name>疯狂JAVA系列</name> <anthor>李刚</anthor> <price>89</price> </book> </bookstore>