Home Xml Locate a Node and Change Its Content

Related Tutorials


 
 

Share on Google+Share on Google+

Locate a Node and Change Its Content

Advertisement
In this section, you will learn how to change the Content of a node in a DOM document.

Locate a Node and Change Its Content

     

This Example shows you how to Change the Content of a node in a DOM document. JAXP (Java API for XML Processing) is an interface which provides parsing of xml documents. Here the Document BuilderFactory is used to create new DOM parsers. These are some of the methods used in code given below for adding attribute:-

Element root = doc.getDocumentElement():-direct access to the root of the DOM document.

NodeList childnodelist = root.getChildNodes():-created a childnodelist and get the collection of child nodes of this node.

Xml code for the program generated is:-

<?xml version="1.0" encoding="UTF-8"?>
<Company>
  <Employee>
  <Employeename>Girish Tewari</Employeename>
  <email>girish@gmail.com</email>
  </Employee>
  <Employee>
  <Employeename>Komal</Employeename>
  <email>Komal@gmail.com</email>
  </Employee>
  <Employee>
  <Employeename>Mahendra</Employeename>
  <email>dhoni@gmail.com</email>
  </Employee>
</Company>

Changecontent.java :-

 

/* 
 * @Program that Locate a Node and Change Its Content
 * UsingSibling.java 
 * Author:-RoseIndia Team
 * Date:-09-Jun-2008
 */

import java.io.*;
import javax.xml.parsers.*;
import org.w3c.dom.*;

public class Changecontent {

  public static void main(String[] args) throws Exception {
  boolean validating = false;
  DocumentBuilderFactory factory = 
  DocumentBuilderFactory.newInstance();

  factory.setValidating(validating);

  Document doc = 
  factory.newDocumentBuilder().parse(
new File("2.xml"));
  new Changecontent().changeContent(doc, 
  
"Girish Tewari""@Rose.com");
}
  public void changeContent(Document doc, 
  String newname, String newemail) {


  Element root = doc.getDocumentElement();
  
  //creating a nodelist
  NodeList childnodelist = root.getChildNodes();
  for (int i = 0; i < childnodelist.getLength(); i++) {
  
  Node childnode = childnodelist.item(i);
  Node subchildname = childnode.getFirstChild();
  Text text = (Text) subchildname.getFirstChild();
  String oldname = text.getData();
  //compares oldname with newname
  if (oldname.equals(newname)) {
  //gets the next node of the subchild
  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());
  }

  }

  }
}

Output of the program:-

email
Old e-mail id: girish@gmail.com
New e-mail id: @Rose.com

DownLoad Source Code

     

Advertisement

If you enjoyed this post then why not add us on Google+? Add us to your Circles



Liked it!  Share this Tutorial


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: August 24, 2008

Related Tutorials

Discuss: Locate a Node and Change Its Content  

Post your Comment


Your Name (*) :
Your Email :
Subject (*):
Your Comment (*):
  Reload Image
 
 
Comments:0
DMCA.com