Delete Node with DOM in Java XML

Create new package named data. In this package, create new xml file named products.xml as below:

<?xml version="1.0" encoding="UTF-8"?>
<products>
	<product>
		<id>p01</id>
		<name>name 1</name>
		<price currency="usd">20</price>
		<quantity>5</quantity>
		<weight unit="kg">2.1</weight>
		<date format="dd/MM/yyyy">02/03/2017</date>
		<status>true</status>
	</product>
	<product>
		<id>p02</id>
		<name>name 2</name>
		<price currency="eur">12</price>
		<quantity>3</quantity>
		<weight unit="kg">6.5</weight>
		<date format="dd/MM/yyyy">24/11/2018</date>
		<status>true</status>
	</product>
	<product>
		<id>p03</id>
		<name>name 3</name>
		<price currency="aud">13</price>
		<quantity>1</quantity>
		<weight unit="kg">7.8</weight>
		<date format="dd/MM/yyyy">14/10/2018</date>
		<status>false</status>
	</product>
</products>




Create new package, named demo. In this package, create new java class, named Main as below

package demo;

import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMSource;
import javax.xml.transform.stream.StreamResult;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;

public class Main {

	private static void delete(String id) {
		String xmlFile = "src\\data\\products.xml";
		try {
			DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
			DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
			Document document = documentBuilder.parse(xmlFile);
			NodeList products = document.getElementsByTagName("product");
			for (int i = 0; i < products.getLength(); i++) {
				Element product = (Element) products.item(i);
				Element idTag = (Element) product.getElementsByTagName("id").item(0);
				if (idTag.getTextContent().equalsIgnoreCase(id)) {
					idTag.getParentNode().getParentNode().removeChild(products.item(i));
					break;
				}
			}
			saveXMLContent(document, xmlFile);
		} catch (Exception e) {
			System.err.println(e.getMessage());
		}
	}

	private static void saveXMLContent(Document document, String xmlFile) {
		try {
			TransformerFactory transformerFactory = TransformerFactory.newInstance();
			Transformer transformer = transformerFactory.newTransformer();
			transformer.setOutputProperty(OutputKeys.INDENT, "yes");
			DOMSource domSource = new DOMSource(document);
			StreamResult streamResult = new StreamResult(xmlFile);
			transformer.transform(domSource, streamResult);
		} catch (Exception ex) {
			System.out.println(ex.getMessage());
		}
	}

	public static void main(String[] args) {

		delete("p02");

	}

}




<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<products>
	<product>
		<id>p01</id>
		<name>name 1</name>
		<price currency="usd">20</price>
		<quantity>5</quantity>
		<weight unit="kg">2.1</weight>
		<date format="dd/MM/yyyy">02/03/2017</date>
		<status>true</status>
	</product>

	<product>
		<id>p03</id>
		<name>name 3</name>
		<price currency="aud">13</price>
		<quantity>1</quantity>
		<weight unit="kg">7.8</weight>
		<date format="dd/MM/yyyy">14/10/2018</date>
		<status>false</status>
	</product>
</products>