0

I'm trying to read a huge XML file(2GB). So, I started of to use STAX and I've to use steam input. Below is the code

filePath = "D:/Data/sample.xml"; //Correct path
String tagContent = null;
XMLInputFactory factory = XMLInputFactory.newInstance();
XMLStreamReader reader = null;
try{
    System.out.println("here");
    reader = factory.createXMLStreamReader(ClassLoader.getSystemResourceAsStream(filePath));
    System.out.println("Reader1:" + reader);
}
catch(Exception e)
{
    System.out.println("Error 4:" + e);
}
System.out.println("Reader2:" + reader);

Output returns error:

here
Error 4:javax.xml.stream.XMLStreamException: java.net.MalformedURLException
Reader2:null

How do I resolve this error?

3
  • 1
    How does filePath look? Commented Mar 4, 2014 at 8:53
  • 1
    I don't like your output. The second "Reader: null" doesn't come from the code you've pasted. Commented Mar 4, 2014 at 8:54
  • I've made some corrections. Commented Mar 4, 2014 at 8:56

4 Answers 4

3

Method getSystemResourceAsStream opens for reading, a resource of the specified name from the search path used to load classes.

You are going to read a file so open a file stream:

        filePath = "D:/Data/sample.xml"; //Correct path
        String tagContent = null;
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLStreamReader reader = null;
        try{
            System.out.println("here");
            reader = factory.createXMLStreamReader(new FileInputStream(new File(filePath)));
            System.out.println("Reader:" + reader);
        }
        catch(Exception e)
        {
            System.out.println("Error 4:" + e);
        }
        System.out.println("Reader:" + reader);
Sign up to request clarification or add additional context in comments.

Comments

0

Following line appears to be throwing an exception

reader = factory.createXMLStreamReader(ClassLoader.getSystemResourceAsStream(filePath));

which causes reader to remain null. Most probably class loading is not successful.

Comments

0

I think you filepath is not a valid URL: try with : file:///D:/Data/sample.xml

1 Comment

Change System.out.println("Error 4:" + e); by System.out.println("Error 4:" + e.getMessage()); to check what is failing
0

ClassLoader.getSystemResourceAsStream(...) is using the system classloader. Just use this.getClass().getResoruceAsStream(...).

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.