I am trying to read large XML file, I want only to read cars owners and I can't load whole xml to memory, how to do that ?
The XML file:
<root>
<message>
<car>
<owner>adam</owner>
</car>
<desk>
<owner>sam</owner>
<game>
<owner>dorothy</owner>
</game>
<pen>
<owner>dorothy</owner>
</pen>
</desk>
</message>
</root>
For example this code does not know exactly what it reads.. how to be sure that we are reading car owners ?
XMLInputFactory xmlInputFactory = XMLInputFactory.newInstance();
XMLEventReader reader = xmlInputFactory.createXMLEventReader(new FileInputStream(entry.toFile()));
while (reader.hasNext()) {
XMLEvent nextEvent = reader.nextEvent();
if (nextEvent.isStartElement()) {
StartElement startElement = nextEvent.asStartElement();
log.info(startElement.getName().toString());
switch (startElement.getName().getLocalPart()) {
case "owner":
// whose owner. .. ?
Sturdy but viable solution is to create a small state machine, capture events as they go and mutate state accordingly