Source Code : Getting Attributes with DOM

Java Is Open Source Programming Language You Can Download From Java and Java Libraries From http://www.oracle.com. Click Here to download
We provide this code related to title for you to solve your developing problem easily. Libraries which is import in this program you can download from http://www.oracle.com. Click Here or search from google with Libraries Name you get jar file related it

Getting Attributes with DOM

         


import java.io.IOException;

import org.w3c.dom.Document;
import org.w3c.dom.NamedNodeMap;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

import com.sun.org.apache.xerces.internal.parsers.DOMParser;

public class MainClass {

  public static void main(String args[]) throws IOException, SAXException {

    DOMParser parser = new DOMParser();
    parser.parse("games.xml");

    Document dom = parser.getDocument();

    NodeList games = dom.getElementsByTagName("game");

    for (int i = 0; i < games.getLength(); i++) {
      Node aNode = games.item(i);
      System.out.println(aNode.getFirstChild().getNodeValue());

      NamedNodeMap attributes = aNode.getAttributes();

      for (int a = 0; a < attributes.getLength(); a++) {
        Node theAttribute = attributes.item(a);
        System.out.println(theAttribute.getNodeName() + "=" + theAttribute.getNodeValue());
      }
    }

  }

}

   
    
    
    
    
    
    
    
  

Thank with us