aspose 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 RabbitMQ in Depth this week in the Open Source 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
Rancher

Joined: Mar 22, 2005
Posts: 42956
    
  73
Does org.w3c.dom.Element.setAttribute(String name, String value) not do what you're trying to do?
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: How to add attribute to tag in XML uing DOM parser