user1919035
user1919035

Reputation: 227

Parse large XML file with java

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?

Upvotes: 0

Views: 814

Answers (4)

Stefan
Stefan

Reputation: 12463

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

Upvotes: 0

m-szalik
m-szalik

Reputation: 3654

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);

Upvotes: 3

Pablo Lozano
Pablo Lozano

Reputation: 10342

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

Upvotes: 0

Tech Agent
Tech Agent

Reputation: 657

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.

Upvotes: 0

Related Questions