fx_199182 2012-10-10 13:56 采纳率: 0%
浏览 241
已采纳

一个android中XML解析的问题。

我遇到一个SAX解析XML文件的问题,求高手指导下。如何将如下XML文件解析:



我是
一个
兵来

老百姓。


士兵



求只要简单写出ContentHandler接口中的处理方法就好,万分感激,求指导教育。

  • 写回答

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]

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题
  • ¥15 HFSS 中的 H 场图与 MATLAB 中绘制的 B1 场 部分对应不上
  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配