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 :-
/*
|
Output of the program:-
Attributes of Id before Adding is: Rose-78 Attributes of Id after Adding is: 05MC34 |