-2

In my Eclipse project, I'm trying to parse an xml file into a String List to display in a jsp file.

My parser works perfectly with this code:

public class XMLReader{
    public static void main(String [] args){
        System.out.println(getXML());
    }
    public static String getXML(){
        try{
            SAXParserFactory factory = SAXParserFactory.newInstance();
            SAXParser saxParser = factory.newSAXParser();
            XMLHandler handler = new XMLHandler(); // class I created extending DefaultHandler
           saxParser.parse("file.xml",handler);
           urls = handler.getXMLList();
        }
        catch(Exception e){
           e.printStackTrace();
        }
        return urls.toString();
    }
}

The string list is printed to my console as desired. But when I try to rewrite the methods to return a string so that I can use it in my jsp file:

public class XMLReader{
    public String returnXML(){
        return getXML().toString();
    }
    public List<String> getXML(){
        ...
    }
}

I receive a java.io.FileNotFoundException. In my console, it shows that Eclipse is trying to find the file in the folder containing my eclipse.exe, rather than in my workspace folder. I've been searching the Internet for a couple hours but I have no idea what's causing to look in a different folder. I'm not sure if making the methods non-static or using a jsp file is causing the file path to change, but I'm only encountering this issue with my saxParser.parse(file,handler) line of code, and I have no clue how to fix it. Any help would be great!

  • Your code says: catch(Excdeption e){ Hence your parser clearly doesn't work perfectly with this code. Please post the actual code. – Evan Knowles Jun 27 '13 at 22:10
  • @EvanKnowles Had a couple typos...thanks for noticing. The parser does work with the code, it just stops working when I try to return a String rather than do a System.out.println. – Matt Tomlinson Jun 28 '13 at 00:48
  • Your code doesn't show the declaration for `file`, or how the file name is set. Those things might be important. – Scott Leis Jun 28 '13 at 01:41
  • @ScottLeis good point. I just have the file name as a string. Edited to reflect it. – Matt Tomlinson Jun 28 '13 at 01:57

1 Answers1

0

I think you just need to specify the full path to the XML file.

If you're calling SAXParser.parse(String,DefaultHandler), the Javadoc says the String is a URI, so for a local file it should be specified as "file://drive/directory/file.xml", using forward-slashes as the directory separator regardless of platform.

Otherwise, you can create a File instance, which allows you to specify a file path in a platform-dependent way (e.g. using backslashes in Windows), and call SAXParser.parse(File,DefaultHandler).

You may want to dynamically determine the location of your Eclipse workspace folder, if that's where the file is. I don't know how to do that but there should be a way.

One final suggestion: naming your class "XMLReader" may be a slightly risky ambiguity because there's a common interface with the same name (org.xml.sax.XMLReader).

Scott Leis
  • 2,810
  • 6
  • 28
  • 42