Java sax program does not go to startElement () method

I am trying to use a sax parser with a custom DefaultHandler, but the strange thing is that the startElement () method in dHandler is never called. endDocument () works as it should, but prints

Total elements:0

while my xml file has 11 status elements.

even if I put total ++ outside the if statement, so it will increment every time startElement () is called, it still says 0 elements.

Please help me with this, thanks

if(e.getSource()==open)
    {
        JFileChooser chooseFile=new JFileChooser();
        int returnVal = chooseFile.showOpenDialog(wnd);
         if (returnVal == JFileChooser.APPROVE_OPTION) {
            File file = chooseFile.getSelectedFile();

                //This is where a real application would open the file.
                parser.parse(new InputSource(new FileInputStream(file)));
                DocumentImpl document = (DocumentImpl)parser.getDocument();
                Node root = document.getLastChild();
                AllElements allelements = new AllElements();
                NodeIteratorImpl iterator =(NodeIteratorImpl)document.createNodeIterator(root,
        NodeFilter.SHOW_ELEMENT, (NodeFilter)allelements, true);
                Node n;
                states.removeAll(states);
                while ((n = iterator.nextNode()) != null)
                {

                    if(n.getNodeName().equals("state"))
                    {
                        NamedNodeMap attrs = n.getAttributes();
                        NodeList children=n.getChildNodes();
                        State newState=new State(attrs.item(0).getNodeValue(),
                                attrs.item(1).getNodeValue(),attrs.item(2).getNodeValue(),attrs.item(3).getNodeValue(),children.item(0).getTextContent());
                        states.add(newState);

                    }

                }
                Collections.sort(states,new StateComparator());
                mod.setRowCount(states.size());
                mod.setColumnCount(5);
                for(int i=0;i<states.size();i++)
                {
                    mod.setValueAt(states.get(i).abbr, i, 0);

                    mod.setValueAt(states.get(i).name, i, 1);
                    mod.setValueAt(states.get(i).population_2k10, i, 2);
                    mod.setValueAt(states.get(i).rank_2k, i, 3);
                    mod.setValueAt(states.get(i).census_1990, i, 4);
                }

                mean=0;
                max=MAXIMUM;
                min=MINIMUM;
                count=0;
                  total=0;
                  SAXParserFactory parserFact = SAXParserFactory.newInstance();
        SAXParser parserS = parserFact.newSAXParser();
        DefaultHandler dHandler = new DefaultHandler(){
            public void startElement(String uri, String name, String element, Attributes atri)
                            {
                if (element.equals("state")){
                    total++;
                }
            }
            public void endDocument(){
                System.out.println("Total elements: " + total);
            }
        };
                  parserS.parse(file, dHandler);
                  meanL.setText("mean="+mean);
                  maxL.setText("max="+max);
                  minL.setText("min="+min);
                  countL.setText("count="+total);
        } else {

        }

     } 
+3
source share
3 answers

Please check the import statement for the Attribute parameter, this should be:

import org.xml.sax.Attributes;

Hi

+16
source

, startElement() . @Override , . , startElement(...) ( ) .

EDIT: startElement :

void startElement(
    String uri, 
    String localName, 
    String qName, 
    Attributes attributes) 

, .

+1

, , - "". , DefaultHandler.startElement :

if (name.equals("state")){
    total++;
}

if (element.equals("state")){
    total++;
}

startElement - qName. , ; , .

Edit

, , , Oracle/Sun Java 6, . DefaultHandler, :

DefaultHandler handler = new DefaultHandler() {

    public int total = 0;

    public void startElement(String uri, String name, String qName,
            Attributes atri) {
        if (qName.equals("state")) {
            total++;
        }
    }

    public void endDocument() {
        System.out.println("Total elements: " + total);
    }
};

, . , , ( ). . DefaultHandler , .

0

All Articles