StAX XML all content between two required tags
Try
StringWriter sw = new StringWriter();
XMLOutputFactory of = XMLOutputFactory.newInstance();
XMLEventWriter xw = null;
XMLInputFactory f = XMLInputFactory.newInstance();
XMLEventReader xr = f.createXMLEventReader(new FileInputStream("test.xml"));
while (xr.hasNext()) {
XMLEvent e = xr.nextEvent();
if (e.isStartElement()
&& ((StartElement) e).getName().getLocalPart().equals("tagINeed")) {
xw = of.createXMLEventWriter(sw);
} else if (e.isEndElement()
&& ((EndElement) e).getName().getLocalPart().equals("tagINeed")) {
break;
} else if (xw != null) {
xw.add(e);
}
}
xw.close();
System.out.println(sw);
prints
<someinternalTag1>
<someinternalTag11>
Some text content..
</someinternalTag11>
</someinternalTag1>
<someinternalTag2>
Something here
</someinternalTag2>
Update:
If you need XML string with too, we can write like that:
if (e.isStartElement() &&
((StartElement) e).getName().getLocalPart().equals("tagINeed")){
xw = of.createXMLEventWriter(sw);
xw.add(e);
} else if (e.isEndElement() &&
((EndElement) e).getName().getLocalPart().equals("tagINeed")){
xw.add(e);
break;
} else if (xw != null) {
xw.add(e);
}