Reputation: 51
i'm trying to use the sax parser with customized DefaultHandler, but the strange thing that startElement() method in the dHandler is never invoked. endDocument() works as it should, but it prints
Total elements:0
while my xml-file has 11 elements of type "state".
if even i place total++ outside the if statement, so it gonna increment everytime startElement() is invoked, it still says 0 elements.
please help me on that, thank you
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 {
}
}
Upvotes: 3
Views: 5800
Reputation: 161
Please check the import statement for the Attribute parameter, it should be:
import org.xml.sax.Attributes;
Regards
Upvotes: 16
Reputation: 76719
I think you meant that the name of the element is 'state'. If so, the following line in the DefaultHandler.startElement implementation should be:
if (name.equals("state")){
total++;
}
instead of
if (element.equals("state")){
total++;
}
The third parameter to the startElement method is actually the qName. It will be empty, if no namespace prefix exists; if a prefix is present, then it will be populated.
Edit
It does not appear that the original code will compile, at least not against the Oracle/Sun Java 6 compiler that I used. Here's what the implementation of the DefaultHandler class ought to look like, if the total elements were to be counted:
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);
}
};
Note that the total variable is now a member of the anonymous class. If it weren't then it has to be declared as final in the outer class, which would make it immutable (rendering the increment operation as illegal). One cannot however, access the total variable from outside the anonymous class though. I would suggest extending the DefaultHandler class in a different class, and to use that handler implementation, to keep things simple.
Upvotes: 0
Reputation: 86774
Maybe you haven't overridden the default startElement()
correctly. If you add an @Override
annotation to the method you should not get a compile-time error. If you do, then the declaration of startElement(...)
(which you didn't show) is incorrect.
EDIT: Your startElement
declaration needs to look like this:
void startElement(
String uri,
String localName,
String qName,
Attributes attributes)
The parameter names can be different, but the number of arguments and their types must match exactly.
Upvotes: 1