SAX解析xml文件实例

前端之家收集整理的这篇文章主要介绍了SAX解析xml文件实例前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

编写一个books.xml文件

<?xml version="1.0" encoding="gb2312" ?>
- < books >
- < book isbn =" 7506342605 ">
< name > 《济公游记》 </ name >
    
< price > 80 </ price >
     
< author > 郭小亭 </ author >
    
< year > 南宋 </ year >
  
</ book >
- < book isbn =" 7506342606 ">
< name > 小张游记 </ name >
    
< price > 80 </ price >
     
< author > 小张 </ author >
    
< year > 南宋 </ year >
  
</ book >
- < book isbn =" 7506342607 ">
< name > 射雕英雄传 </ name >
    
< price > 80 </ price >
     
< author > 刘鑫 </ author >
    
< year > 南宋 </ year >
  
</ book >
- < book isbn =" 7506342608 ">
< name > 金瓶梅 </ name >
    
< price > 89 </ price >
     
< author > 陈智 </ author >
    
< year > 南宋 </ year >
  
</ book >
</ books >

然后定义一个工具类MySax.java

import java.io.File;

import javax.xml.parsers.ParserConfigurationException;
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 MySax extends DefaultHandler {
private String qName;
private boolean tag;
public static void main(String[] args) throws Exception {
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser parser = factory.newSAXParser();
MySax m = new MySax();
parser.parse(new File("thinkmore/books.xml"),m);
}
public void startDocument() throws SAXException {
}
public void startElement(String uri,String localName,String qName,
Attributes attributes) throws SAXException {
if(qName.equals("book")){
System.out.print(attributes.getValue("isbn")+"\t");
}
this.qName=qName;
this.tag=true;

}

实现类TestSax.java

import java.io.File;
import java.util.LinkedList;

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 TestSax extends DefaultHandler {
private String qName;
private boolean tag;
private LinkedList<Book> list= new LinkedList<Book>();
public static void main(String[] args) throws Exception {
SAXParserFactory factory=SAXParserFactory.newInstance();
SAXParser parser=factory.newSAXParser();
TestSax handler=new TestSax();
parser.parse(new File("thinkmore/books.xml"),handler);
}
@Override
public void startDocument() throws SAXException {
}
@Override
public void characters(char ch[],int start,int length)
throws SAXException {
String str=new String(ch,start,length);

if(this.qName.equals("price")&&tag){
System.out.print(str+"\t");
}
if(this.qName.equals("name")&&tag){
System.out.print(str+"\t");
}
if(this.qName.equals("author")&&tag){
System.out.print(str+"\t");
}
if(this.qName.equals("year")&&tag){
System.out.print(str+"\t");
}
}
@Override
public void startElement(String uri,
Attributes attributes) throws SAXException {
if(qName.equals("book")){
System.out.print(attributes.getValue("isbn")+"\t");
}
this.tag=true;
this.qName=qName;

}
@Override
public void endElement(String uri,String qName2)
throws SAXException {
if(qName2.equals("book")){
System.out.println();
}
this.tag=false;
}
@Override
public void endDocument() throws SAXException {

}
}

public void characters(char ch[],int length) throws SAXException { String str = new String(ch,length); if(this.qName.equals("name")){ System.out.print(str+"\t"); } if(this.qName.equals("price")){ System.out.print(str+"\t"); } if(this.qName.equals("author")){ System.out.print(str+"\t"); } if(this.qName.equals("year")){ System.out.print(str+"\t"); } } public void endElement(String uri,String qName) throws SAXException { if(this.qName.equals("book")){ System.out.println(); } this.tag=false; } public void endDocument() throws SAXException { } }

原文链接:https://www.f2er.com/xml/299997.html

猜你在找的XML相关文章