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:
- Whether your function is static or not.
- 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