Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 3 of 3

Thread: Update Xml Using DOM objects

  1. #1
    Junior Member
    Join Date
    Sep 2014
    Posts
    2
    My Mood
    Amused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Update Xml Using DOM objects

    Hi I have a requirement to insert the data into xml file . I have used DOM class for doing this.

    Below is my xml file


    <?xml version="1.0" encoding="ISO-8859-1" standalone="no"?>
    <deployments>
    <applications>
    <application>
    <name> PCMH</name>
    <envs>
    <env>
    <name> DEVA</name>
    <version>1.0.0 </version>
    <components> </components>
    </env>
    <env>
    <name> DEVB</name>
    <version>4.0.0 </version>
    <components> </components>
    </env>	
    <env>
    <name> SITA</name>
    <version>3.0.0 </version>
    <components> </components>
    </env>
    <env>
    <name> SITB</name>
    <version>1.0.0 </version>
    <components> </components>
    </env>
    <env>
    <name> UAT</name>
    <version>2.0.0 </version>
    <components> </components>
    </env>
    <env>
    <name> TT</name>
    <version>1.0.0 </version>
    <components> </components>
    </env>
    <env>
    <name> PROD</name>
    <version>1.0.0 </version>
    <components> </components>
    </env>
    </envs>
    </application>
    <newNode>value</newNode>
    <newNode>value</newNode>
    <newNode>value</newNode>
    <newNode>value</newNode>
    <application>
    <name> PROVIDER</name>
    <envs>
    <env>
    <name> DEVA</name>
    <version>1.0.0 </version>
    <components> </components>
    </env>
    <env>
    <name> DEVB</name>
    <version>1.0.0 </version>
    <components> </components>
    </env>	
    <env>
    <name> SITA</name>
    <version>1.0.0 </version>
    <components> </components>
    </env>
    <env>
    <name> SITB</name>
    <version>1.0.0 </version>
    <components> </components>
    </env>
    <env>
    <name> UAT</name>
    <version>1.0.0 </version>
    <components> </components>
    </env>
    <env>
    <name> TT</name>
    <version>1.0.0 </version>
    <components> </components>
    </env>
    <env>
    <name> PROD</name>
    <version>1.0.0 </version>
    <components> </components>
    </env>
    </envs>
    </application>
     
    </applications>
    </deployments>


    The thing is if SITA is the name then I have to insert component to the particular envi tabs , if DEVB is the component I have to insert the component there. How can I do this I have done some code its inserting the data at the bottom og the XML .Please help.
    Last edited by prathesh.bv; September 30th, 2014 at 09:41 AM.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,520
    My Mood
    Mellow
    Thanks
    215
    Thanked 697 Times in 679 Posts

    Default Re: Update Xml Using DOM objects

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    Please post your code correctly using code or highlight tags per the above link.

    And you've been through the DOM Tutorial?

  3. #3
    Junior Member
    Join Date
    Sep 2014
    Posts
    2
    My Mood
    Amused
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: Update Xml Using DOM objects

    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");
    		}
    	}
     
    }

Similar Threads

  1. [SOLVED] Write an xml dom to a xml file
    By Kakashi in forum File I/O & Other I/O Streams
    Replies: 2
    Last Post: March 2nd, 2011, 02:30 PM
  2. DOM XML parser question
    By LDM91 in forum Java Theory & Questions
    Replies: 3
    Last Post: December 30th, 2010, 07:34 AM
  3. XML DOM Parser problem
    By kanishktew in forum What's Wrong With My Code?
    Replies: 0
    Last Post: April 10th, 2010, 09:42 PM
  4. How to read an XML document in Java with DOM Parse?
    By JavaPF in forum File Input/Output Tutorials
    Replies: 0
    Last Post: May 20th, 2008, 07:04 AM
  5. How to read an XML document in Java with DOM Parse?
    By JavaPF in forum Java Code Snippets and Tutorials
    Replies: 0
    Last Post: May 20th, 2008, 07:04 AM