java解析xml之sax解析xml示例分享
package com.test;
import java.io.File;
import java.io.FileInputStream;
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;
public class SaxXML {
public static void main(String[] args) {
File file = new File("e:/People.xml");
try {
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser parser = spf.newSAXParser();
SaxHandler handler = new SaxHandler("People");
parser.parse(new FileInputStream(file), handler);
List<People> peopleList = handler.getPeoples();
for(People people : peopleList){
System.out.println(people.getId()+"\t"+people.getName()+"\t"+people.getEnglishName()+"\t"+people.getAge());
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class SaxHandler extends DefaultHandler {
private List<People> peoples = null;
private People people;
private String currentTag = null;
private String currentValue = null;
private String nodeName = null;
public List<People> getPeoples() {
return peoples;
}
public SaxHandler(String nodeName) {
this.nodeName = nodeName;
}
@Override
public void startDocument() throws SAXException {
// TODO 當(dāng)讀到一個(gè)開始標(biāo)簽的時(shí)候,會(huì)觸發(fā)這個(gè)方法
super.startDocument();
peoples = new ArrayList<People>();
}
@Override
public void endDocument() throws SAXException {
// TODO 自動(dòng)生成的方法存根
super.endDocument();
}
@Override
public void startElement(String uri, String localName, String name,
Attributes attributes) throws SAXException {
// TODO 當(dāng)遇到文檔的開頭的時(shí)候,調(diào)用這個(gè)方法
super.startElement(uri, localName, name, attributes);
if (name.equals(nodeName)) {
people = new People();
}
if (attributes != null && people != null) {
for (int i = 0; i < attributes.getLength(); i++) {
if(attributes.getQName(i).equals("id")){
people.setId(attributes.getValue(i));
}
else if(attributes.getQName(i).equals("en")){
people.setEnglishName(attributes.getValue(i));
}
}
}
currentTag = name;
}
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO 這個(gè)方法用來處理在XML文件中讀到的內(nèi)容
super.characters(ch, start, length);
if (currentTag != null && people != null) {
currentValue = new String(ch, start, length);
if (currentValue != null && !currentValue.trim().equals("") && !currentValue.trim().equals("\n")) {
if(currentTag.equals("Name")){
people.setName(currentValue);
}
else if(currentTag.equals("Age")){
people.setAge(currentValue);
}
}
}
currentTag = null;
currentValue = null;
}
@Override
public void endElement(String uri, String localName, String name)
throws SAXException {
// TODO 在遇到結(jié)束標(biāo)簽的時(shí)候,調(diào)用這個(gè)方法
super.endElement(uri, localName, name);
if (name.equals(nodeName)) {
peoples.add(people);
}
}
}
相關(guān)文章
Java web項(xiàng)目中的強(qiáng)制登錄功能實(shí)現(xiàn)代碼
本文給大家分享Java web項(xiàng)目中的強(qiáng)制登錄功能實(shí)現(xiàn)代碼,為了避免直接進(jìn)入項(xiàng)目中存在的頁面,使用filter過濾器,代碼簡單易懂,對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友參考下吧2021-11-11
SpringBoot整合阿里?Druid?數(shù)據(jù)源的實(shí)例詳解
這篇文章主要介紹了SpringBoot整合阿里?Druid?數(shù)據(jù)源,主要講解了手動(dòng)配置方法,結(jié)合實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-11-11
Java修改maven的默認(rèn)jdk版本為1.7的方法
這篇文章主要介紹了Java修改maven的默認(rèn)jdk版本為1.7的方法,需要的朋友可以參考下2018-02-02
Java日期時(shí)間字符串和毫秒相互轉(zhuǎn)換的方法
這篇文章主要為大家詳細(xì)介紹了Java日期時(shí)間字符串和毫秒相互轉(zhuǎn)換的方法,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12
Java的Jackson庫的使用及其樹模型的入門學(xué)習(xí)教程
這篇文章主要介紹了Java的Jackson庫的使用及其樹模型入門學(xué)習(xí)教程,Jackson庫通常被用來作Java對(duì)象和JSON的互相轉(zhuǎn)換,需要的朋友可以參考下2016-01-01
Java數(shù)據(jù)結(jié)構(gòu)篇之實(shí)現(xiàn)二叉搜索樹的核心方法
二叉搜索樹是一種常用的數(shù)據(jù)結(jié)構(gòu),它是一棵二叉樹,且每個(gè)節(jié)點(diǎn)的值都大于其左子樹中任何節(jié)點(diǎn)的值,而小于其右子樹中任何節(jié)點(diǎn)的值,這篇文章主要給大家介紹了關(guān)于Java數(shù)據(jù)結(jié)構(gòu)篇之實(shí)現(xiàn)二叉搜索樹的核心方法,需要的朋友可以參考下2023-12-12

