Results 1 to 4 of 4

Thread: Change the Content of Node by Locating it

  1. #1
    Join Date
    Nov 2009
    Posts
    580

    Change the Content of Node by Locating it

    Hi, I want the program which will help to locate the node and then to change the content of the node. If anyone have it, please give it to me.

  2. #2
    Join Date
    Oct 2005
    Posts
    2,393

    Re: Change the Content of Node by Locating it

    Hi, I think the code given below will help you to get Change the content of node by locating it. Make use of it to get what you want.

    Code:
    import java.io.*;
    import javax.xml.parsers.*;
    import org.w3c.dom.*;
    
    public class ContentChange
     {
    
        public static void main(String[] args) throws Exception
     {
            boolean validating = false;
            DocumentBuilderFactory dbfactory = DocumentBuilderFactory.newInstance();
            dbfactory.setValidating(validating);
    
            Document dcmt = dbfactory.newDocumentBuilder().parse(new File("2.xml"));
            new Changecontent().ContentChange(dcmt, "Rohan Gaikwad", "@Techarena.in");
    }
        public void ContentChange(Document dcmt, String newname, String newemail)
     {
            Element elmnt = dcmt.getDocumentElement();
            NodeList childnodelist = elmnt.getChildNodes();
            for (int j = 0; j < childnodelist.getLength(); j++)
     {
                
                Node childnode = childnodelist.item(j);
                Node subchildname = childnode.getFirstChild();
                Text text = (Text) subchildname.getFirstChild();
                String oldname = text.getData();
                if (oldname.equals(newname))
     {
                    subchildname=subchildname.getNextSibling();
                    System.out.println(subchildname.getNodeName());
                    text =(Text)subchildname.getFirstChild();
                    System.out.println("Old e-mail id: "+text.getData());
                    text.setData(newemail);
                    System.out.println("New e-mail id: "+text.getData());
                }
    
            }
    
        }
    }

  3. #3
    Join Date
    May 2008
    Posts
    2,389

    Re: Change the Content of Node by Locating it

    Hi, I don't have much knowledge about it, But from the internet I have got the code which will help to change the content of node by locating it. Just look at it, if it is helpful to you or not.
    Code:
    public void changeContent(Document doc,String newname,String newemail) 
    {
        Element elroot = doc.getDocumentElement();
        NodeList rootlist = elroot.getChildNodes();
        for(int j=0; j<rootlist.getLength(); j++) 
    {
            Element person = (Element)rootlist.item(j);
            NodeList personlist = person.getChildNodes();
            Element elname = (Element)personlist.item(0);
            NodeList namelist = elname.getChildNodes();
            Text nametext = (Text)namelist.item(0);
            String oldname = nametext.getData();
            if(oldname.equals(newname)) 
    {
                Element email = (Element)personlist.item(1);
                NodeList emaillist = email.getChildNodes();
                Text emailtext = (Text)emaillist.item(0);
                emailtext.setData(newemail);
            }
        }
    }

  4. #4
    Join Date
    Feb 2008
    Posts
    1,852

    Re: Change the Content of Node by Locating it

    Hi, I am also searching for the same code which will help me to change the content of Node by Locating it. If you are able to get it then please give it to me also. I have searched on internet but didn't got much useful information about it. If you able to get methods which can be useful to you in this case, then you can give me that also, I can check whether it will helpful to me or not. If I am able to get information about this, then I will provide you that.

Similar Threads

  1. How to change color of selected node in JTree?
    By Juaquine in forum Software Development
    Replies: 5
    Last Post: 04-02-2012, 09:02 PM
  2. Locating MAC Address On iPhone
    By GurpreetC in forum Networking & Security
    Replies: 6
    Last Post: 20-05-2010, 01:37 AM
  3. Converting CDATA Node into Text Node
    By Jagdish Gada in forum Software Development
    Replies: 3
    Last Post: 07-12-2009, 09:54 AM
  4. Replies: 3
    Last Post: 28-09-2009, 02:54 PM
  5. How to fix error locating \3\lxcrtime.dll
    By BYRNE in forum Networking & Security
    Replies: 3
    Last Post: 26-05-2009, 10:03 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Page generated in 1,750,194,551.65291 seconds with 16 queries