File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
http://aspose.com/file-tools
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


Win a copy of EJB 3 in Action this week in the EJB and other Java EE Technologies forum!
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: 39535
    
  27
Does org.w3c.dom.Element.setAttribute(String name, String value) not do what you're trying to do?


Ping & DNS - updated with new look and Ping home screen widget
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: How to add attribute to tag in XML uing DOM parser
 
Similar Threads
creating a new xml document with dom
How to add Attributes to a existing XML File.
JSP and XML
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