I am not sure if this is possible so anyways thought of asking and getting clarified as I was unable to find anything similar to this here even after a lot of searches and trying out things. Really sorry if found a duplicate.
I am trying to parse the XML file using the XMLStreamReader
. After reading I have to make few checks for my localpart
based on which I need to assign this XMLStreamReader
to different classes.
When I am making few checks I need to check the current localpart
as well as next localpart
. When I try to check the next localpart
I am advancing to next
element on XMLStreamReader
. So I am trying to store the current XMLStreamReader
within another temp XMLStreamReader
but after the assignment when I try to navigate to next on the current XMLStreamReader
then even the temp XMLStreamReader
is changing I guess due to which I am unable to perform some of the action. Mainly I would like to perform JAXB Unmarshalling
during which it's failing as its unable to get all the elements of the class.
I am aware the XMLEventReader
has peek()
but that is also failing so I am trying to store the current XMLStreamReader
but its also not working and I read that its not most memory efficient. As my XML file can be very large I am trying to use the XMLStreamReader
.
public class Main
{
public static void main(String[] args) {
final InputStream inputStream = Main.class.getClassLoader().getResourceAsStream("Customer.xml");
final XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
final XMLStreamReader xmlStreamReader = xmlInputFactory.createXMLStreamReader(xmlStream);
//final JAXBContext jaxbContext = JAXBContext.newInstance("io.model.jaxb");
//final Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
//Navigate to next and start of the XML Elements
xmlStreamReader.next();
//Read Until the end of the file
while (xmlStreamReader.hasNext()) {
//Check if the current `XML Tag` is "extension"
if (xmlStreamReader.isStartElement() && xmlStreamReader.getLocalName().equalsIgnoreCase("extension")) {
//Create a tempStream to store the current stream values
//final XMLStreamReader streamReader = xmlInputFactory.createXMLStreamReader((InputStream) xmlStreamReader);
final XMLStreamReader tempReader = xmlStreamReader;
xmlStreamReader.next();
//If the next element is "nextextension"
if (xmlStreamReader.isStartElement() && xmlStreamReader.getLocalName().equalsIgnoreCase("nextextension")) {
System.out.println("Double Extension Assign to CLASS C with both extension tag");
//final C cInfo = unmarshaller.unmarshal(streamReader, C.class).getValue();
}else{
System.out.println("Single Extension Assign to CLASS B with single extension tag");
//final B cInfo = unmarshaller.unmarshal(streamReader, B.class).getValue();
}
}
//Move to the next
xmlStreamReader.next();
}
}
}
I hope I was able to explain the issue. All I want to know if there is a way to achieve any of the below:
- Temporarily store the
current XMLStreamReader
in anothertemp XMLStreamReader
so that it can be used later. - If there is any other way to
peek()
into thenext localpart
withinXMLStreamReader
.
It would be really nice to get some inputs from experts here as I am stuck in this issue since yesterday.
Posting this answer as it can be useful to someone in the future. After trying a lot and researching I found we can store the stream in
MutableXMLStreamBuffer
.Then you can access the same using following: