我遇到一个SAX解析XML文件的问题,求高手指导下。如何将如下XML文件解析:
我是
一个
兵来
自
老百姓。
士兵
求只要简单写出ContentHandler接口中的处理方法就好,万分感激,求指导教育。
[code="java"]package XML;
import java.io.File;
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.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class SAXParserXML {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SAXParserFactory factory = SAXParserFactory.newInstance();
try {
SAXParser parser = factory.newSAXParser();
File file = new File(System.getProperty("user.dir") + "/content.xml");
parser.parse(file, new PersonXMLHandler());// 此处的file可以使文件、输入流、或URL字符串
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class Content{
String type;
String name;
String description;
List subcontents;
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDescription() {
return description;
}
public void setDescription(String description) {
this.description = description;
}
public List getSubcontents() {
return subcontents;
}
public void setSubcontents(List subcontents) {
this.subcontents = subcontents;
}
@Override
public String toString() {
return "Content [description=" + description + ", name=" + name
+ ", subcontents=" + subcontents.size() + ", type=" + type + "]";
}
}
class PersonXMLHandler extends DefaultHandler {
Content content;
String temp = "";
String currenttag = null;
List subcontents;
int level=0;
Content subcontent;
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
if (currenttag != null) {
String value = new String(ch, start, length);
if (currenttag.equals("decript")) {
temp += value;// 处理长文本
}
}
}
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
super.endDocument();
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// TODO Auto-generated method stub
currenttag=null;
if (qName.endsWith("decript")) {
if(level!=0){
subcontent.setDescription(temp);
}else{
content.setDescription(temp);
}
temp = "";
}
if(qName.endsWith("content")){
if(level==0){
content.setSubcontents(subcontents);
subcontents=null;
}else if(level==1){
subcontents.add(subcontent);
subcontent=null;
}
}
}
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
super.startDocument();
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
if (qName.equals("content")) {
content = new Content();
content.setName(attributes.getValue("name"));
content.setType(attributes.getValue("type"));
if(level==0)
level++;
else{
subcontents=new ArrayList<Content>();
subcontent=new Content();
content.setName(attributes.getValue("name"));
content.setType(attributes.getValue("type"));
}
}
currenttag = qName;
}
}[/code]
参考下我的博文[url]http://fengjianjian007-qq-com.iteye.com/admin/blogs/1562059[/url][code="java"]package xmlHandler;
import java.io.File;
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.SAXException;
import org.xml.sax.helpers.DefaultHandler;
public class SAXParserXML {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
SAXParserFactory factory = SAXParserFactory.newInstance();
try {
SAXParser parser = factory.newSAXParser();
File file = new File(System.getProperty("user.dir") + "/person.xml");
parser.parse(file, new PersonXMLHandler());// 此处的file可以使文件、输入流、或URL字符串
} catch (ParserConfigurationException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (SAXException e) {
// TODO Auto-generated catch block
e.printStackTrace();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
class PersonXMLHandler extends DefaultHandler {
Person person;
String temp = "";
String currenttag = null;
@Override
public void characters(char[] ch, int start, int length)
throws SAXException {
// TODO Auto-generated method stub
if (currenttag != null) {
String value = new String(ch, start, length);
if (currenttag.equals("name")) {
person.setName(value);
}
if (currenttag.equals("description")) {
temp += value;// 处理长文本
}
}
}
@Override
public void endDocument() throws SAXException {
// TODO Auto-generated method stub
super.endDocument();
}
@Override
public void endElement(String uri, String localName, String qName)
throws SAXException {
// TODO Auto-generated method stub
currenttag=null;
if (qName.endsWith("description")) {
person.setDescription(temp);
temp = "";
}
if (qName.endsWith("person"))
System.out.println("当前解析出的结果:" + person.toString());
}
@Override
public void startDocument() throws SAXException {
// TODO Auto-generated method stub
super.startDocument();
}
@Override
public void startElement(String uri, String localName, String qName,
Attributes attributes) throws SAXException {
// TODO Auto-generated method stub
if (qName.equals("person")) {
person = new Person();
person.setSex(attributes.getValue("sex"));
}
currenttag = qName;
}
}[/code]