Я решил использовать подход, подобный этому ответу { ссылка }
Спасибо
private static void filter(InputStream fileInputStream, final Set<String> prodIdToExclude) throws SAXException, TransformerException, FileNotFoundException {
XMLReader xr = new XMLFilterImpl(XMLReaderFactory.createXMLReader()) {
private boolean skip;
@Override
public void startElement(String uri, String localName, String qName, Attributes atts)
throws SAXException {
if (qName.equals("Product")) {
String prodId = atts.getValue("Prod_id");
if (prodIdToExclude.contains(prodId)) {
skip = true;
} else {
super.startElement(uri, localName, qName, atts);
skip = false;
}
} else {
if (!skip) {
super.startElement(uri, localName, qName, atts);
}
}
}
public void endElement(String uri, String localName, String qName) throws SAXException {
if (!skip) {
super.endElement(uri, localName, qName);
}
}
@Override
public void characters(char[] ch, int start, int length) throws SAXException {
if (!skip) {
super.characters(ch, start, length);
}
}
};
Source src = new SAXSource(xr, new InputSource(fileInputStream));
Result res = new StreamResult(new FileOutputStream("output.xml"));
TransformerFactory.newInstance().newTransformer().transform(src, res);
}