wood burning stoves 2.0*
The moose likes Web Services and the fly likes How to add attribute to tag in XML uing DOM parser Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Java » Web Services
Bookmark "How to add attribute to tag in XML uing DOM parser" Watch "How to add attribute to tag in XML uing DOM parser" New topic
Author

How to add attribute to tag in XML uing DOM parser

Muthu Prasad
Greenhorn

Joined: Aug 04, 2005
Posts: 2
Hello,
I am using DOMXML parser i have added sample code
======================================================================
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
// Instead of parsing an XML document, however, just create an empty
// document that you can build up yourself.
Document document = db.newDocument();

// Now build a document tree using DOM methods
Element book = document.createElement("book"); // Create new element
book.setAttribute("id", "javanut4"); // Give it an attribute
document.appendChild(book); // Add to the document
for(int i = 1; i <= 3; i++) { // Add more elements
Element chapter = document.createElement("chapter");
Element title = document.createElement("title");
title.appendChild(document.createTextNode("Chapter " + i));
chapter.appendChild(title);
chapter.appendChild(document.createElement("para"));
book.appendChild(chapter);
}
==========================================================================
in this how can add a attribute for a Tag?
Ulf Dittmer
Marshal

Joined: Mar 22, 2005
Posts: 41106
    
  45
Does org.w3c.dom.Element.setAttribute(String name, String value) not do what you're trying to do?


Ping & DNS - my free Android networking tools app
 
Don't get me started about those stupid light bulbs.
 
subject: How to add attribute to tag in XML uing DOM parser
 
Similar Threads
JSP and XML
How to add Attributes to a existing XML File.
creating a new xml document with dom
Can anyone help?I still can't append records in xml...
how to read complex xml document where element has attributes and sub element using java dom parser