Results 1 to 15 of 15
  1. #1
    prathesh.bv is offline Member
    Join Date
    Sep 2014
    Posts
    14
    Rep Power
    0

    Default Xml Editing 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


    <code>
    -<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>
    -<envs>
    -</application>
    -</applications>
    -</deployments>


    </code>


    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.

  2. #2
    unsensible is offline Member
    Join Date
    Apr 2014
    Posts
    14
    Rep Power
    0

    Default Re: Xml Editing Using DOM objects

    Hmm. Seeing how you are using the DOM I would assume that you are using javascript to populate this xml? If that is the case, this is the wrong forum.

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Xml Editing Using DOM objects

    Quote Originally Posted by unsensible View Post
    Hmm. Seeing how you are using the DOM I would assume that you are using javascript to populate this xml? If that is the case, this is the wrong forum.
    Well, Java does have a specs for XML processing, both DOM and SAX.

    As for the OP, what does your code look like and what does the output look like, and what should it look like?

    When posting code please wrap it in [code] tags [/code].
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  4. #4
    prathesh.bv is offline Member
    Join Date
    Sep 2014
    Posts
    14
    Rep Power
    0

    Default Re: Xml Editing Using DOM objects

    Hi Thanks for your update below is the XML file and Java Code

    Java Code:
    <?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>
    Java Code:
    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);
     
    		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)));
     
    		        }
    		        }}
     
    			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");
    		}
    	}
     
    }
    Last edited by Tolls; 10-02-2014 at 09:17 AM. Reason: Remove commented out parts due to buggy forum software

  5. #5
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Xml Editing Using DOM objects

    You missed an important bit:
    "what does the output look like, and what should it look like"
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  6. #6
    prathesh.bv is offline Member
    Join Date
    Sep 2014
    Posts
    14
    Rep Power
    0

    Default Re: Xml Editing Using DOM objects

    Hi If the input of environment name is "PCMH" , envi name is DEVA and version number is 1.0.0 then i have to add the component to that particular node

    Java Code:
    <?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>component1 </components>
    <components>component2 </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>
    Then if the environment name is "PCMH" , envi name is DEVB and version number is 1.0.0 the component should be add in DEVB node
    Last edited by prathesh.bv; 10-02-2014 at 03:18 PM.

  7. #7
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Xml Editing Using DOM objects

    And?
    "what does the output look like, and what should it look like"

    You've shown us the latter, but not the former.

    Seeing what the problem is helps us narrow down where the problem is.
    You can't expect us to actually run this thing?
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  8. #8
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    13,541
    Rep Power
    26

    Default Re: Xml Editing Using DOM objects

    I just removed the commented out bits of the code you posted as the forum has a bug around large commented blocks.
    Helps to see what the code is actually doing.
    Please do not ask for code as refusal often offends.

    ** This space for rent **

  9. #9
    prathesh.bv is offline Member
    Join Date
    Sep 2014
    Posts
    14
    Rep Power
    0

    Default Re: Xml Editing Using DOM objects

    To be clear i have edited the expecting output xml.

    Below is the actual output im expecting. I will pass application name , Envi , version , component to java

    1. if application name = PCMH , Envi = SITA and Version=1.0.0

    Then the component is to be add in that particular portion like below
    <name> PCMH</name>
    <envs>
    <env>
    <name> SITA</name>
    <version>1.0.0 </version>
    <components>Component1</components>
    </env>

    1. if application name = PROVIDER, Envi = DEVA and Version=1.0.0

    Then the component should be add in below part of the xml file
    <application>
    <name> PROVIDER</name>
    <envs>
    <env>
    <name> DEVA</name>
    <version>1.0.0 </version>
    <components>Component1 </components>
    </env>

  10. #10
    jim829 is offline Senior Member
    Join Date
    Jan 2013
    Location
    Northern Virginia, United States
    Posts
    6,226
    Rep Power
    13

    Default Re: Xml Editing Using DOM objects

    I noticed that you are comparing to "DEVA" in the code. But it looks like the data field of the tags is " DEVA" which includes
    a white space. Could that be contributing to your problem?

    Edit: Never mind. I just noticed the trim() method at the end of the statement.

    Regards,
    Jim
    Last edited by jim829; 10-02-2014 at 03:55 PM.
    The JavaTM Tutorials | SSCCE | Java Naming Conventions
    Poor planning on your part does not constitute an emergency on my part

  11. #11
    prathesh.bv is offline Member
    Join Date
    Sep 2014
    Posts
    14
    Rep Power
    0

    Default Re: Xml Editing Using DOM objects

    Hi Comparing the string is not a issue. Here the issue is im not able to insert the component into the particular node. But the same time I can able to insert the data in the bottom of the xml file.

  12. #12
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Xml Editing Using DOM objects

    I'm still having great difficulty understanding what exactly you need to achieve.

    Based on the input XML and your expected output XML I suspect it is as simple as this: for a given environment, set the value of the components child element.

    In other words in the input XML the components child element is empty and you have the job of filling it with a specific value. Am I correct?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  13. #13
    prathesh.bv is offline Member
    Join Date
    Sep 2014
    Posts
    14
    Rep Power
    0

    Default Re: Xml Editing Using DOM objects

    Hi first one is correct .

    for a given environment, set the value of the components child element.

  14. #14
    gimbal2 is offline Just a guy
    Join Date
    Jun 2013
    Location
    Netherlands
    Posts
    5,114
    Rep Power
    12

    Default Re: Xml Editing Using DOM objects

    That's already very different than what you have been trying to do and are asking questions about - trying to add new nodes. Apparently you want to achieve one of two things depending on the situation:

    1. if an env parent node with the correct name has a components child node, replace the value child of that existing components node

    or

    2. if the env parent node with the correct name does not have a components child node yet, add a new components child node

    This robustly covers all possible cases and makes no assumptions about the existence of a components child element in the source XML.

    Still correct?
    "Syntactic sugar causes cancer of the semicolon." -- Alan Perlis

  15. #15
    prathesh.bv is offline Member
    Join Date
    Sep 2014
    Posts
    14
    Rep Power
    0

    Default Re: Xml Editing Using DOM objects

    The Component child node has to be add it will not be there in the xml . Once the Application name and Envi name matches we have to add new component node there.

Similar Threads

  1. Something to do with text editing
    By raj87ng in forum New To Java
    Replies: 3
    Last Post: 07-16-2014, 11:35 AM
  2. editing a file. Am I doing this right?
    By Mokomi in forum New To Java
    Replies: 2
    Last Post: 10-14-2011, 04:40 AM
  3. Editing objects within a LinkedList?
    By Slobdell in forum New To Java
    Replies: 2
    Last Post: 07-19-2011, 09:09 PM
  4. Replies: 1
    Last Post: 01-22-2009, 04:25 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •