Two Laptop Bag
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
Register / Login
JavaRanch » Java Forums » Java » Web Services
Reply 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: 32767
Does org.w3c.dom.Element.setAttribute(String name, String value) not do what you're trying to do?


Android appsImageJ pluginsJava web charts
 
 
subject: How to add attribute to tag in XML uing DOM parser
 
Threads others viewed
Can anyone help?I still can't append records in xml...
creating a new xml document with dom
how to read complex xml document where element has attributes and sub element using java dom parser
JSP and XML
How to add Attributes to a existing XML File.
WebSphere development made easy
without the weight of IBM tools
http://www.myeclipseide.com