G
G
Glavryba2014-08-04 12:42:23
Java
Glavryba, 2014-08-04 12:42:23

How to parse multiple XML files using SAX in Java?

I can't start parsing several XML files from one folder
. Here is the program code:
Here I upload files

public static void main(String[] args)  throws IOException
    {
        SAXParserFactory factory = SAXParserFactory.newInstance();

    factory.setValidating(true);
    factory.setNamespaceAware(false);
    SAXParser parser;

    //InputStream xmlData = null;
    try
    {
        BufferedReader xmlData = new BufferedReader(new InputStreamReader(new FileInputStream("D:\\XMLExamples")));

        parser = factory.newSAXParser();
        parser.parse(xmlData, new MyParser());


    } 

}

Error occurs when passing arguments to parser.parse Error text: Exception in thread "main" java.lang.Error: Unresolved compilation problem:
The method parse(InputStream, HandlerBase) in the type SAXParser is not applicable for the arguments (BufferedReader, MyParser )

The class in which the parser is processed:
Override
public void startElement(String uri, String localName, String qName,
        Attributes attributes) throws SAXException {
    System.out.println("Тег: "+qName);
    if(qName.equals("HEADER"))
        System.out.println("IDFILE: "+attributes.getValue("IDFILE"));
    System.out.println("S_ID: "+attributes.getValue("S_ID"));
    //      System.out.println(attributes.getLength());
    super.startElement(uri, localName, qName, attributes);


}
  @Override
    public void characters(char[] c, int start, int length) 
                                                 throws SAXException {
        super.characters(c, start,  length);
        for(int i=start;i< start+length;++i)
        System.err.print(c[i]);
}

@Override
public void endElement(String uri, String localName, String qName) 
                                                throws SAXException {

    System.out.println("Tag done: "+qName);
    super.endElement(uri,localName, qName);
}

@Override
public void startDocument() throws SAXException {
    System.out.println("Start parsing doc!");
    super.startDocument();
}

@Override
public void endDocument() throws SAXException {
    super.endDocument();
    System.out.println("Parsing finished!");

}

}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vasily, 2014-08-04
@Applez

Please show the MyParser class in full.
You may find it more convenient to use XMLReader instead of BufferReader.

SAXParser saxParser = spf.newSAXParser();
XMLReader xmlReader = saxParser.getXMLReader();
xmlReader.parse(convertToFileURL(filename));

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question