0

So I know this is a quite popular issue however non of the solution avaialable worked for me.

My code is a sample code for reading and parsing xml document. The orriginal source is in this link: http://www.studytrails.com/java/xml/woodstox/java-xml-stax-woodstox-basic-parsing.jsp

The relative path for my java file is

 src\com\parser\xml\TestBasicStaxParsing.java

The input file "employee.xml" is in the same depth as src & bin folder and .classpath file.

BTW I am using eclipse in a windows environment if this will help you.

Following is the java file I am trying to run

package com.parser.xml;

import java.io.InputStream;

import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.events.XMLEvent;

import org.codehaus.stax2.XMLInputFactory2;
import org.codehaus.stax2.XMLStreamReader2;

public class TestBasicStaxParsing {

private void execute(String xmlFileName) throws Exception {

    InputStream xmlInputStream = getClass().getResourceAsStream(xmlFileName);
    XMLInputFactory2 xmlInputFactory = (XMLInputFactory2)XMLInputFactory.newInstance();
    XMLStreamReader2 xmlStreamReader = (XMLStreamReader2) xmlInputFactory.createXMLStreamReader(xmlInputStream);
    while(xmlStreamReader.hasNext()){
        int eventType = xmlStreamReader.next();
        switch (eventType) {
        case XMLEvent.START_ELEMENT:
            System.out.print("<"+xmlStreamReader.getName().toString()+">");
            break;
        case XMLEvent.CHARACTERS:
            System.out.print(xmlStreamReader.getText());
            break;
        case XMLEvent.END_ELEMENT:
            System.out.println("</"+xmlStreamReader.getName().toString()+">");
            break;
        default:
            //do nothing
            break;
        }
    }

}

public static void main(String[] args) throws Exception {
    (new TestBasicStaxParsing()).execute("employee.xml");
}

}

Up to my understanding it seems that there are multiple alternatives for the following line of code depending on:

  1. Whether your function is static or not.
  2. Extension of your input file

EDIT

 getClass().getResourceAsStream(xmlFileName)

But I am not sure how it should be manipulated and why

Any suggestions ? Thanks in advance

Abdelrahman Shoman
  • 2,882
  • 7
  • 36
  • 61

1 Answers1

0

As it says in the JavaDoc the null value happens in two cases: - null xmlFileName (so check that you are actually passing the right argument) - the resource cannot be found.

In the second case, it seems you are tryign to get the file that is 'next' to your TestBasicStaxParsing.java code in some directory under the soruce.

Make sure that the xmlFileName is the complete name of the file, with the extension and without any other path parts so for example simple MyFile.xml not some ../project/src/com/parser/xml/MyFile.xml;

Check that the file is actually copied to the build folder but your bild script (so it should be also next to your TestBasicStaxParsing.class file)

Zielu
  • 8,312
  • 4
  • 28
  • 41