Modify XML File in Java using DOM parser
In this tutorial we show how can you read and modify the contents of an XML File using a DOM parser.
- We are going to use to get the elements of the document with specific tag name.
- Use of the element’s attributes. to get a
- Use to get a specific attributes by name.
- Use to set the value of that specific attributes.
- Use or in order to remove or add a new property for the specific element.
You download the jar file named as jdom-2.0.4.jar
/*
* Author : Rajkumar Sahoo
* */
package Pack;
import java.io.File;
import java.io.IOException;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerException;
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.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;
public class ReadAndModifyXMLFile {
public static final String xmlFilePath = "D:\\employees.xml";
public static void main(String argv[]) {
try {
DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder documentBuilder = documentBuilderFactory.newDocumentBuilder();
Document document = documentBuilder.parse(xmlFilePath);
// Get employee by tag name
//use item(0) to get the first node with tage name "employee"
Node employee = document.getElementsByTagName("employee").item(0);
System.out.println("employee :"+employee);
// update employee , set the id to 10
NamedNodeMap attribute = employee.getAttributes();
Node nodeAttr = attribute.getNamedItem("id");
nodeAttr.setTextContent("2");
// append a new node to the first employee
Element address = document.createElement("address");
address.appendChild(document.createTextNode("Noida sec-62"));
employee.appendChild(address);
// loop the employee node and update salary value, and delete a node
NodeList nodes = employee.getChildNodes();
for (int i = 0; i < nodes.getLength(); i++) {
Node element = nodes.item(i);
if ("salary".equals(element.getNodeName())) {
element.setTextContent("20000");
}
// remove firstname
if ("firstname".equals(element.getNodeName())) {
employee.removeChild(element);
}
}
// write the DOM object to the file
TransformerFactory transformerFactory = TransformerFactory.newInstance();
Transformer transformer = transformerFactory.newTransformer();
DOMSource domSource = new DOMSource(document);
StreamResult streamResult = new StreamResult(new File(xmlFilePath));
transformer.transform(domSource, streamResult);
System.out.println("The XML File was ");
} catch (ParserConfigurationException pce) {
pce.printStackTrace();
} catch (TransformerException tfe) {
tfe.printStackTrace();
} catch (IOException ioe) {
ioe.printStackTrace();
} catch (SAXException sae) {
sae.printStackTrace();
}
}
}
0 comments:
Post a Comment