Implementing the setAttribute() method of DOM API


 

Implementing the setAttribute() method of DOM API

In this section, you will see the implemention of the setAttribute() method of DOM API

In this section, you will see the implemention of the setAttribute() method of DOM API

Implementing the setAttribute() method of DOM API

In this illustration, we will discuss about the appending the node to the existing XML document.

In this program, we create the object of DocumentBuilderFactory to parse the xml document. Before parsing the file it needs to support the basic XML Recommendation called wellformedness of xml document. We use setValidating(false) method and we include the method setExpandEntityReferences(false) to avoid the expansions of the entities.

After parsing the document, create the new node by createElement("string") and append to the root node of the document.

The setAttribute("name","value") method is used to set the attribute of the node. The TransformerFactory class is used to print the xml document on the console.

The code of the program is as follows:

order.xml: input file

<?xml version="1.0" encoding="UTF-8"?>           
<orders>
   <order>     
       <item >
           <name>Premium</name>
           <price>49.00</price>
           <qty>15</qty>
       </item>
       <item>
           <name>blanket</name>
           <price>20</price>
           <qty>10</qty>
       </item>
   </order>
</orders>

setattributeExample.java file.

import java.io.File;
import org.w3c.dom.Comment;
import org.w3c.dom.Document;
import org.w3c.dom.Element;
import javax.xml.parsers.*; 
import javax.xml.transform.*; 
import javax.xml.transform.dom.DOMSource; 
import javax.xml.transform.stream.StreamResult;
import javax.xml.parsers.DocumentBuilderFactory;
public class setattributeExample
{
public static void main(String[] argvthrows Exception {
  try{
     DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
     factory.setValidating(false);
     factory.setExpandEntityReferences(false);
     Document doc = factory.newDocumentBuilder().parse(new File("order.xml"));
     Element node = doc.getDocumentElement();
     Element snode=doc.createElement("SALE");
     Comment comment = doc.createComment("invalid -- comment");
     node.appendChild(snode);      // add a child node
     snode.setAttribute("Books","123");   //attribute to the child node
     snode.appendChild(comment);      // comment in the child node body
    TransformerFactory tranFactory = TransformerFactory.newInstance()
    Transformer aTransformer = tranFactory.newTransformer()
    Source src = new DOMSource(doc)
    Result dest = new StreamResult(System.out)
    aTransformer.transform(src, dest)
   }catch(Exception e){}
 }
}

Output:

C:\>javac setattributeExample.java

C:\>java setattributeExample
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
<orders>
         <order>
                  <item >
                       <name>Premium</name>
                       <price>49.00</price>
                       <qty>15</qty>
                 </item>
              <item>
                     <name>blanket</name>
                     <price>20</price>
                    <qty>10</qty>
            </item>
       </order>
     <SALE   Books="123">
           <!--invalid - - comment-->
     </SALE>
</orders>

Download the java Code.

Download the xml Code.

Ads