我已经按照在线教程创建了2个类(子类和主类),通过扩展DefaultHandler
类来使用SAX解析器读取XML文件。一切都很好但是当我尝试在Eclipse中编写方法SAXParser.parse(String URI, DefaultHandler object)
时,它显示在下面的错误图像。
请帮助我在这里做错了。
public class mainclass{
public static void main(String []args)
{
DefaultHandler dff=new subclass();
SAXParserFactory spf=SAXParserFactory.newInstance();
try
{
SAXParser sp=spf.newSAXParser();
sp.parse("Trial.xml", dff);
}
catch(Exception e)
{
System.out.println("exception is "+e);
}
}
}
public class subclass extends DefaultHandler{
boolean bheader=false;
boolean bdetail=false;
boolean blpn=false;
public void startDocument() throws SAXException
{
System.out.println("Starting the start document method");
}
public void endDocument() throws SAXException
{
System.out.println("End document method called");
}
public void startElement(String uri,String localName,String qName,Attributes attributes) throws SAXException
{
System.out.println("Start element method called "+qName);
}
public void endElement(String uri,String localName,String qName) throws SAXException
{
System.out.println("end element method called "+qName);
}
public void characters(char ch[],int start,int length)
{
System.out.println("Characters method called "+new String(ch,start,length));
}
}
答案 0 :(得分:1)
您正在从错误的包导入DefaultHandler类。你应该导入org.xml.sax.helpers.DefaultHandler