SAXParser' startElement'中的参数方法始终为null

时间:2017-07-13 07:02:23

标签: java xml saxparser

我正在尝试实现一个sax解析器来加载一些XML。但是,DefaultHandler中的方法中的所有参数都始终为null。例如,在System.out.println(uri);方法中调用startElement会打印出null。代码如下。运行代码时,不会在任何地方打印异常/错误。任何想法/帮助都表示赞赏。

默认处理程序:

    try {

        handler = new DefaultHandler(){
            int channel_amt = 0;

            public void startElement(String uri, String localname, String name, Attributes attributes) throws SAXException {

                if(name == "fixture"){
                    profileID++;
                    System.out.println(attributes.getValue(0));
                    profile_name = attributes.getValue(0);
                    profile_mode = attributes.getValue(1);
                    profile_channels.clear();
                } else {

                    if(name == "channel"){

                        profile_channel = new Vector<Object>();
                        profile_channel.addElement(attributes.getValue(0));
                        profile_channel.addElement(attributes.getValue(1));
            //          profile_channel.addElement(Boolean.parseBoolean(attributes.getValue(2)));

                        channel_amt++;
                        if(Arrays.asList(channels).indexOf(attributes.getValue(1)) != -1){
                            profile_channel_function[Arrays.asList(channels).indexOf(attributes.getValue(1))] = channel_amt;
                        }

                    } else {
                        profile_channel.addElement(new Range(Integer.parseInt(attributes.getValue(0)), Integer.parseInt(attributes.getValue(1)), attributes.getValue(2)));
                    }

                } 

            }       
            public void endElement(String uri, String localname, String name) throws SAXException {

                if(name == "fixture"){
                    profile[profileID] = new Profile(profile_name, profile_mode, profile_channels, profile_built_in_dimmer, profile_channel_function);
                } else if(name == "channel"){
                    profile_channels.add(profile_channel);
                }

            }
        };

        loadProfile("Dimmer.xml");

    } catch(Exception e){
        e.printStackTrace();
    }

loadProfile方法:

    public void loadProfile(String name){
        for(int c=0;c<51;c++){
            profile_channel_function[c] = 0;
        }

        SAXParser parser;
        try {
            parser = factory.newSAXParser();
            parser.parse(new File(name), handler);
        } catch (Exception e) {
            e.printStackTrace();
        }

    }

提前谢谢你。

0 个答案:

没有答案