1

I am using XMLEventReader for parsing big xml files and it works fine in one project but when I import the same class with the same imports in another project it does not parse the xml well and it gives STAX Event.

Below is my code:

    XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLEventReader eventReader = factory.createXMLEventReader(new FileReader(exportedFilePath));

I debugged and inspected the eventReader and in the project where is working is f in property and in the project where does not work there is m.

enter image description here

enter image description here

I can not catch the exception because it is same class and same imports..

Here are the imports:

import javax.xml.stream.XMLEventReader;
import javax.xml.stream.XMLInputFactory;
import javax.xml.stream.XMLStreamConstants;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.events.Attribute;
import javax.xml.stream.events.Characters;
import javax.xml.stream.events.EndElement;
import javax.xml.stream.events.Namespace;
import javax.xml.stream.events.StartElement;
import javax.xml.stream.events.XMLEvent;
Bambus
  • 1,493
  • 1
  • 15
  • 32
  • Classpath problem? It might be caused by another class with the same name loaded prior to your class (it may be even within the framework or in the runtime environment) – gaborsch Jan 25 '19 at 10:46
  • I do not understand what is the problem. I understand you get two different implementations (Woodstox in the first project, and the JDK's default implementation in the second), I can explain to you why this happens (e.g. https://stackoverflow.com/questions/17807123/how-to-override-a-service-provider-in-java), but I do not understand what is your question/issue... Could you edit the question to tell us ? – GPI Jan 25 '19 at 11:17

2 Answers2

1

This should help:

System.setProperty("javax.xml.stream.XMLInputFactory", "com.sun.xml.internal.stream.XMLInputFactoryImpl");
xmlParser
  • 1,903
  • 4
  • 19
  • 48
0

It may be casued by a Classpath problem.

It might be that another class with the same name loaded prior to your class (it may be even within the framework or in the runtime environment) that prevents loading your referred class, for example a different version of your stax parser.

You have three options:

  • Try to find out the version of StAX parser that is present in that environment, and compile yourt code against it

  • Use an own classloader/container for your lib, that will guarantee that the proper version is loaded,

  • try to reorder tha classpath that your lib comes first - though it might corrupt the system elsewhere, so I don't really recommend.

gaborsch
  • 15,408
  • 6
  • 37
  • 48