Hi GregBrannon , Thanks for your reply please find the code here
package com.cf.pam;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.io.StringWriter;
import java.util.Properties;
import java.util.StringTokenizer;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.transform.OutputKeys;
import javax.xml.transform.Result;
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.Node;
import org.w3c.dom.NodeList;
import org.w3c.dom.Text;
public class UpdateXmlPoc {
public static void main(String args[]) throws IOException {
FileReader reader = new FileReader(
"H:\\Automation\\TestRobo\\DomainDetails.properties");
Properties properties = new Properties();
properties.load(reader);
// Properties properties=new Properties(reader);
/*String Application_Name = properties.getProperty("Application_Name");
String Component_Name = properties.getProperty("Component_Name");
String Release_Version = properties.getProperty("Release_Version");
String Environment_Name = properties.getProperty("Environment_Name");
StringTokenizer tokenizer = new StringTokenizer(Release_Version, ":");
String key = tokenizer.nextToken();
String Val = tokenizer.nextToken();*/
try {
DocumentBuilderFactory docFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder docBuilder = docFactory.newDocumentBuilder();
Document doc = docBuilder
.parse("H:\\Automation\\TestRobo\\applications.xml");
doc.getDocumentElement().normalize();
System.out.println(("Root element :" + doc.getDocumentElement().getNodeName()));
NodeList nList = doc.getElementsByTagName("application");
for (int temp = 0; temp < nList.getLength(); temp++) {
Node nNode = nList.item(temp);
System.out.println("\nCurrent Element :" + nNode.getNodeName());
if (nNode.getNodeType() == Node.ELEMENT_NODE) {
Element eElement = (Element) nNode;
String InsrtNod = eElement.getElementsByTagName("name").item(0).getTextContent();
System.out.println("Node set to insert is "+InsrtNod);
String Compare=eElement.getElementsByTagName("name").item(1).getTextContent().trim();
System.out.println("Node to compare"+Compare);
if(Compare.equals("DEVA")){
System.out.println("Enter");
Text a = doc.createTextNode("value");
Element p = doc.createElement("newNode");
p.appendChild(a);
nList.item(1).getLastChild().insertBefore(p, nList.item(1));
System.out.println("Value to Insert"+nList.item(1).getLastChild().insertBefore(p, nList.item(1)));
}
/*System.out.println("First Name : " + eElement.getElementsByTagName("name").item(0).getTextContent());
System.out.println("First Name : " + eElement.getElementsByTagName("name").item(1).getTextContent());
System.out.println("First Name : " + eElement.getElementsByTagName("version").item(0).getTextContent());
System.out.println("First Name : " + eElement.getElementsByTagName("components").item(0).getTextContent());*/
}}
/*Node earth = doc.getFirstChild();
NodeList List=doc.getChildNodes();
System.out.println("Nodes List is " + List);
NamedNodeMap earthAttributes = earth.getAttributes();
Attr galaxy = doc.createAttribute("galaxy");
galaxy.setValue("milky way");
earthAttributes.setNamedItem(galaxy);
Node ApplicationName = doc.createElement("ApplicationName");
ApplicationName.setTextContent(Application_Name);
earth.appendChild(ApplicationName);
Node ComponentName = doc.createElement("ComponentName");
ComponentName.setTextContent(Component_Name);
earth.appendChild(ComponentName);
Node ReleaseVersion = doc.createElement("ReleaseVersion");
ReleaseVersion.setTextContent(key);
earth.appendChild(ReleaseVersion);
Node EnvironmentName = doc.createElement("EnvironmentName");
EnvironmentName.setTextContent(Environment_Name);
earth.appendChild(EnvironmentName);*/
Transformer transformer = TransformerFactory.newInstance()
.newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "yes");
// initialize StreamResult with File object to save to file
StreamResult result = new StreamResult(new StringWriter());
DOMSource source = new DOMSource(doc);
// transformer.transform(source, result);
String xmlString = result.getWriter().toString();
Result dest = new StreamResult(new File(
"H:\\Automation\\TestRobo\\applications.xml"));
System.out.println(xmlString);
transformer.transform(source, dest);
} catch (Exception e) {
// TODO: handle exception
System.out.println(e + "Exception Block");
}
}
}