我遇到一个SAX解析XML文件的问题,求高手指导下。如何将如下XML文件解析:
我是
一个
兵来
自
老百姓。
士兵
求只要简单写出ContentHandler接口中的处理方法就好,万分感激,求指导教育。
一个android中XML解析的问题。
- 写回答
- 好问题 0 提建议
- 追加酬金
- 关注问题
- 邀请回答
-
2条回答 默认 最新
- FangXingXing007 2012-10-11 16:11关注
[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]
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 请问有用MZmine处理 “Waters SYNAPT G2-Si QTOF质谱仪在MSE模式下采集的非靶向数据” 的分析教程吗
- ¥50 opencv4nodejs 如何安装
- ¥15 adb push异常 adb: error: 1409-byte write failed: Invalid argument
- ¥15 nginx反向代理获取ip,java获取真实ip
- ¥15 eda:门禁系统设计
- ¥50 如何使用js去调用vscode-js-debugger的方法去调试网页
- ¥15 376.1电表主站通信协议下发指令全被否认问题
- ¥15 物体双站RCS和其组成阵列后的双站RCS关系验证
- ¥15 复杂网络,变滞后传递熵,FDA
- ¥20 csv格式数据集预处理及模型选择