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.
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.
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()); } } } }
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); } } }
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.
Bookmarks