Home | Ajax | BioInformatics | Dojo | EAI | EJB | Hibernate | J2ME | Java | Java Glossary | Java Servlets | JavaScript | Jboss | JDBC | JDO | Jmeter | JSF | JSP | JUnit | Maven | MySQL | Spring Framework | SQL | Struts | Technology | WAP | Web Services | XML


 
  
 
Programming Tutorials: Ajax | Articles | JSP | Bioinformatics | Database | Free Books | Hibernate | J2EE | J2ME | Java | JavaScript | JDBC | JMS | Linux | MS Technology | PHP | RMI | Web-Services | Servlets | Struts | UML
 

 
Facing Programming Problem?
Ask Questions?, Browse Latest Questions, Question-Answer Guidelines
XML
  JDO Tutorials
  EAI Articles
  Struts Tutorials
  Java Tutorials
  Java Certification
  Java Applet
Questions
Comments

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

                         

Leave your comment:

Name:

Email:

URL:

Title:

Comments:


Enter Code:

Audio Version
Reload Image
 

Note: Emails will not be visible or used in any way, and are not required. Please keep comments relevant. Any content deemed inappropriate or offensive may be edited and/or deleted.

No HTML code is allowed. Line breaks will be converted automatically. URLs will be auto-linked. Please use BBCode to format your text.

Add This Tutorial To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Latest Searches:
Java Communications AP
��?��??��?
java Lang package
Aะ�?ะ�?ะ�?
Display Data from Data
breakะà¸?ะ????ะà
DOM
comparator example
e books images
Refresh a Web Page Usi
date adjust timezone
jsf Tutorials
connecting to database
.getCurrencyInstance
Date Examples java
jdbc accessing ms acce
Java programs
JTable Only Accept int
request.setAttribute
iframe
javascript drag and dr
program in java to ins
create .csv file using
java binary trees
Update in jsp
struts-nested select
jsp mysql combo box
goto statment
spring hibernate web
Combattons la programm
java webserver
examples with list
match move in After ef
matrix subtraction
java bean array
href
make keyboard characte
string variable
java buffer
Hash Function in Java
creating new table
using the action liste
tracking technology
h:message
Auto boxing in java
compare two class
Login Application
db2 in roseindia
for loop
print a new line
data structures
how to changepassword
jsf richfaces taglib
login java
wizard in java
Code for buttons on a
verify password in jav
Java Count Vowels
Properties
Servlet Example To Dis
break?°???????°?????
Photoshop Textures and
Photoshop Effects Crea
connection class
concat
Second-generation aspe
what is the kvm
jsp programs with mapp
distributed systems
java / program for lif
use dropdownlist in js
Boolean
j2sdk1.4 features
appfuse
)How will you host an
how to parse
disable minimize in fr
tabbing through a flex
Java Swing Package exa
Developing Struts Hibe
Java String toLowerCase Example
Java String toCharArray Example
Java String substring Example
Java String indexOf Example
Java String startsWith Example
Java String hashCode Example
Java String matches Example
Java String length Example
Java String lastIndexOf Example
Java String isEmpty Example
Java String equalsIgnoreCase Example
Java String equals Example
Java String endsWith Example
Java String copyValueOf Example
Java String contentEquals Example
  EAI Articles
  Java Certification
Tell A Friend
Your Friend Name
Search Tutorials

 

 
 
Browse all Java Tutorials
Java JSP Struts Servlets Hibernate XML
Ajax JDBC EJB MySQL JavaScript JSF
Maven2 Tutorial JEE5 Tutorial Java Threading Tutorial Photoshop Tutorials Linux Technology
Technology Revolutions Eclipse Spring Tutorial Bioinformatics Tutorials Tools SQL
 

Home | JSP | EJB | JDBC | Java Servlets | WAP  | Free JSP Hosting  | Search Engine | News Archive | Jboss 3.0 tutorial | Free Linux CD's | Forum | Blogs

About Us | Advertising On RoseIndia.net  | Site Map

India News

Indian Software Development Company | iPhone Development Company in India | Java Training Delhi | Java Training at Noida |

Send your comments, Suggestions or Queries regarding this site at roseindia_net@yahoo.com.

Copyright © 2008. All rights reserved.