Update Nodes and Attributes 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 use update value for price tag, status tag and currency attribute for product have id is p02

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 update(String id, String newCurrency, double newPrice, boolean newStatus) {
		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);
				if (product.getElementsByTagName("id").item(0).getTextContent().equalsIgnoreCase(id)) {

					// Update value of price tag and currency attribute
					Element priceTag = (Element) product.getElementsByTagName("price").item(0);
					priceTag.setTextContent(String.valueOf(newPrice));
					priceTag.setAttribute("currency", newCurrency);

					// Update value of status tag
					Element statusTag = (Element) product.getElementsByTagName("status").item(0);
					statusTag.setTextContent(String.valueOf(newStatus));

					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) {

		update("p02", "euro", 999, false);

	}

}




<?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>p02</id>
		<name>name 2</name>
		<price currency="euro">999.0</price>
		<quantity>3</quantity>
		<weight unit="kg">6.5</weight>
		<date format="dd/MM/yyyy">24/11/2018</date>
		<status>false</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>
	<product>
		<id>p04</id>
		<name>Name 4</name>
		<price currency="usd">6.5</price>
		<quantity>6</quantity>
		<weight unit="kg">2.0</weight>
		<date format="dd/MM/yyyy">24/03/2018</date>
		<status>true</status>
	</product>
</products>