| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 610 人关注过本帖
标题:事件处理器,断点跟踪到characters方法为什么程序不朝下跑了?
只看楼主 加入收藏
近佛心更静
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2012-6-20
结帖率:100%
收藏
 问题点数:0 回复次数:1 
事件处理器,断点跟踪到characters方法为什么程序不朝下跑了?
1.新建一个Student类,成员变量name,location,grade(为了方便,grade可以是String)
2.xml文件,exam.xml
<?xml version="1.0" encoding="UTF-8"?>
<exam>
    <student>
        <name>李四</name>
        <location>沈阳</location>
        <grade>59</grade>
    </student>
    <student>
        <name>的</name>
        <location>队伍</location>
        <grade>34343</grade>
    </student>
</exam>

3.问题代码
public class Xml封装对象返回List {

    @Test
    public void Test() throws Exception, SAXException {
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser sp = factory.newSAXParser();
        XMLReader reader = sp.getXMLReader();
        MyHandle h = new MyHandle();
        reader.setContentHandler(h);
        reader.parse("src/exam.xml");
        List<Student> list = h.getList();
        System.out.println(list.get(1));
    }

    class MyHandle extends DefaultHandler {
        private List list = new ArrayList();// 解析到一本书就放在list集合里面
        private String currentTag;// 记住解析到的是什么标签
        private Student student;

        public List getList() {
            return list;
        }

        public void startElement(String uri, String localName, String name,
                Attributes attributes) throws SAXException {
            currentTag = name;
            if (currentTag.equals("student")) {
                student = new Student();
            }
        }

        public void characters(char[] ch, int start, int length)
                throws SAXException {
            if (currentTag.equals("name")) {
                String names = new String(ch, start, length);
                student.setNane(names);
            }
            if (currentTag.equals("location")) {
                String location = new String(ch, start, length);
                student.setLocation(location);
            }
            if (currentTag.equals("grade")) {
                String grade = new String(ch, start, length);
                student.setGrade(grade);
            }
        }

        public void endElement(String uri, String localName, String named)
                throws SAXException {
            if (named.equals("student")) {
                list.add(student);
            }
            currentTag = null;
        }
    }
}
搜索更多相关主题的帖子: 跟踪 void version 处理器 public 
2012-06-28 17:16
近佛心更静
Rank: 1
等 级:新手上路
帖 子:5
专家分:0
注 册:2012-6-20
收藏
得分:0 
错误是空指针异常,异常原因还是知道,但是不知道为什么跟踪到currentTag=null时,程序不跑了。
2012-06-28 17:19
快速回复:事件处理器,断点跟踪到characters方法为什么程序不朝下跑了?
数据加载中...
 
   



关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.016911 second(s), 9 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved