Core Java| JSP| Servlets| XML| EJB| JEE5| Web Services| J2ME| Glossary| Questions?

 

 

 

 

 

 

 

 

 

 

 

 

 

Search Tutorials

Latest Questions
Comments
 
Adding an Attribute in DOM Document 
 

In this section, you will learn how to adds an attribute in a DOM document.

 

Adding an Attribute in DOM Document

                         

This Example shows you how to adds an attribute 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. There are some of the methods used in code given below for adding attribute:-

getDocumentElement():- allows direct access to the root node of the document.

root.getAttribute("Id"):-allows to retrieve attributes on  Id.
root.setAttribute("Id", "05MC34"):-allows to set o5Mc34 on the Id

Xml code for the program generated is:-

<?xml version="1.0" encoding="UTF-8"?>
<Author Type='Bible' Id='Rose-78' Issue='1995'>
</Author>

Addingattribute.java :-

 

/* 
 * @Program that Adds an Attribute to an Element
 * Addingattribute.java 
 * Author:-RoseIndia Team
 * Date:-09-Jun-2008
 */

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

public class Addingattribute {

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

        Document doc = factory.newDocumentBuilder().parse(new File("Document3.xml"));
        new Addingattribute().addAttribute(doc);
    }

    public void addAttribute(Document doc) {
        Element root = doc.getDocumentElement();
        System.out.print("Attributes of Id before Adding is: ");
        System.out.println(root.getAttribute("Id"));
        //Adds an attributes to the element ISBN
        root.setAttribute("Id""05MC34");
        System.out.print("Attributes of Id after Adding is: ");
        System.out.println(root.getAttribute("Id"));
}
}


Output of the program:-

Attributes of Id before Adding is: Rose-78
Attributes of Id after Adding is: 05MC34


Download Source Code

                         

» View all related tutorials
Related Tags: .net c xml string http com file orm form xsd schema reference io mail namespace get version type default dtd

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 
Training Courses
Tell A Friend
Your Friend Name
Software Solutions
Least Viewed
Most Rated
Recently Viewed
Search Tutorials

 

 
 

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 | Flex 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.