日期:2014-05-20  浏览次数:20905 次

用SAXparser解析xml文件时老是提示找不到文件,我以经把路径给它了啊
Java code

package com.iotek.xml.studentXML;

import java.io.File;
import java.util.ArrayList;
import java.util.List;


public class TestStudentSAXParser {

    /**
     * @param args
     */
    public static void main(String[] args) {
        List<Student> students=new ArrayList<Student>();
            
            String xmlpathString="E:\\Documents and Settings\\Administrator\\Workspaces\\MyEclipse 8.5\\my_java\\Student.xml";
            System.out.println("读取到的XML文件的路径为:  " + xmlpathString);
            File file=new File(xmlpathString);
            StudentReader studentReader=new StudentReader();
            studentReader.read(file);
        }
        

}

------------------------------------------------------------------------
package com.iotek.xml.studentXML;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.ArrayList;

import javax.xml.parsers.ParserConfigurationException;
import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.Locator;
import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;



public class StudentReader extends DefaultHandler {

    
    
    public void read(File file){

        SAXParserFactory factory=SAXParserFactory.newInstance();
        
        SAXParser parser=null;
        try {
            parser = factory.newSAXParser();
        } catch (ParserConfigurationException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
        
        
        /*InputStream is=null;
        try {
            is = new FileInputStream(file);
        } catch (FileNotFoundException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }*/
        StudentReader dh=new StudentReader();
        [color=#FF0000]try {
            parser.parse(file, dh);在这里提示找不到文件
        } catch (SAXException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }
    }[/color]

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        
    }

    @Override
    public void endDocument() throws SAXException {
        System.out.println("end parse the document");
    }

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

    @Override
    public void endPrefixMapping(String prefix) throws SAXException {
        
    }

    @Override
    public void ignorableWhitespace(char[] ch, int start, int length)
            throws SAXException {
        
    }

    @Override
    public void processingInstruction(String target, String data)
            throws SAXException {
        
        
    }

    @Override
    public void setDocumentLocator(Locator locator) {
        
    }

    @Override
    public void skippedEntity(String name) throws SAXException {
        
    }

    @Override
    public void startDocument() throws SAXException {
    
        System.out.println("start to parse the document");
    }

    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes atts) throws SAXException {
        
        
    }

    @Override
    public void startPrefixMapping(String prefix, String uri)
            throws SAXException {
        
        
    }

}







------解决方案--------------------