2 Answer(s)      8 years ago
Posted in : Java Beginners


View Answers

May 26, 2008 at 7:39 PM

Hi friend,

import org.w3c.dom.*;
import javax.xml.parsers.*;

public class GetNodeExample{
public static void main(String[] args) {
BufferedReader buff = new BufferedReader(new InputStreamReader(;
System.out.print("Enter xml file name: ");
String str = buff.readLine();
File file = new File(str);
DocumentBuilderFactory fact = DocumentBuilderFactory.newInstance();
DocumentBuilder build = fact.newDocumentBuilder();
Document doc = build.parse(str);
Node node = doc.getDocumentElement();
String root = node.getNodeName();
System.out.println("Root Node: " + root);
System.out.println("File not found!");
catch(Exception e){
System.out.println("Exception file not fount" +e);



Read for more information.

May 26, 2008 at 7:47 PM


document = parse("";);
WriteOutput( "(" & getNodeType(document) & ") " & getNodeName(document) & "<br />");
// Root Element (rootElement)
rootElement = getDocumentElement(document);
rootElementAttrs = getAttributes(rootElement);
children = getChildNodes(rootElement);

WriteOutput("(" & getNodeType(rootElement) & ") <code>" & getNodeName(rootElement) & "</code> ");
WriteOutput("(attrs: " & getLength(rootElementAttrs) & ", children: " & getLength(children) & ") <Br />");

// Children of Root Element (thisChild, grandchildren)
for(i = 0; i LT getLength(children) ; i = i + 1) {
thisChild = getChild(children,i);
thisChildAttrs = getAttributes(thisChild);
grandchildren = getChildNodes(thisChild);

if ( getNodeType(thisChild) is "ELEMENT_NODE" ) {
WriteOutput("(" & getNodeType(thisChild) & ") <code>" & getNodeName(thisChild) & "</code> ");
WriteOutput("(attrs: " & getLength(thisChildAttrs) & ", children: " & getLength(grandchildren) & ") <Br />");

Related Tutorials/Questions & Answers:
DOM API - Java Beginners
DOM API  How can i go through each and every node of an xml file Each and everyChild? my xml file is given below: 810 32 141 22
DOM API - Java Beginners
DOM API  How can i go through each and every node of an xml file Each and everyChild? my xml file is given below...); } } } ---------------------------------------------- Read for more information.
Implementing the createTextNode() method in DOM API.
Implementing the createTextNode() method in DOM API In this illustration, we... new DOM parsers and  DocumentBuilder is used in code given below for creating an Blank DOM Document. The following code is used to create a textNode. Text
Example of getDocType method in DOM API.
getDocType() Example in DOM API In this section, we will discuss about the implementation of the getDocType() method. This method checks that DTD(Document... parse the xml file using parse() method and create a DOM Tree. Invoke the object
Implementing 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
Example of getTagName() method in xml dom api.
the element. In this example, we create the Dom parser by create the instance
DOM  package name for Document class?   Hi Friend, The package is known as dom api. For more information, visit the following links: Thanks
DOM  hello, What is DOM?   hii, DOM is stands for Document object Model. DOM is an interface-oriented Application Programming Interface. We can use it for navigation of the entire document
DOM Parser Tutorial
. It is available in package org.w3c.dom. Dom is a cross-language API. It provides many... tree elements. With the help of Dom api, user  can add, delete.... DOMImplementation It is a inter face of DOM API.  It provides various method
DOM to SAX and SAX to DOM - XML
Java DOM to SAX and SAX to DOM  Simple API for XML and Document Object Model
API  what means API in java?   Hi Friend, The Application Programming Interface (API) is a library of functions that allow programmers... for building software applications in java. If you want to know about java api, please
api  WHAT IS THE DEFINITON OF API,EXPLAIN IN DETAIL?   An API is the interface implemented by an application which allows other... of it by a computer program. An API differs from an application binary interface
DOM to SAX and SAX to DOM - XML
DOM SAX Parser Java  What is the Difference between SAX and DOM parsers
Creating Blank DOM Document
Creating Blank DOM Document       This tutorial shows you how to create blank DOM document. JAXP (Java API for XML Processing) is a Java interface that provides a standard
DOM  Hi... I created an xml file through java by using DOM Now..I need to get the prompt for download when i clicked the button... I used downloadaction...for this But it didn't worked.. Its jus viewing the xml file
JAXP API using DOM Parser
JAXP API using DOM Parser      ... based on the structure type.   DOM (Document Object Model) The XML Document Object Model (XML DOM) defines a standard way to access
Dom in java
Dom in java  Exception in thread "main" org.w3c.dom.DOMException: INVALIDCHARACTERERR: An invalid or illegal XML character is specified... Create XML using DOM import*; import org.w3c.dom.*; import
DOM  Hello.... I'm creating an xml file from java using DOM... from the database values... and saving it in a file But the created xml file is generating all its files in a single line.. But i need to get the next tag
Javascript DOM
generate a fixture list. I was knocking about using DOM techniques but with minimal
javascript dom error-message
javascript dom error-message  javascript dom error-message
dom to xml string
dom to xml string  Hi, I am writing a program in java that reads... have save the dom document into xml file. How to convert dom to xml string... for converting DOM object into string data. Please check the thread dom to xml
Create an Empty DOM Document
Create an Empty DOM Document       This Example shows how to create an empty DOM Document . JAXP (Java API for XML Processing) is an interface which provides parsing of xml
jUSB api
jUSB api  How can i access the USB using jUSB api and also hw to add this api to the netbeans
Collections API
Collections API  hello, What is the Collections API?   hi The Collections API is a set of classes and interfaces that support operations on collections of objects
API for beginners
API for beginners  what is api? why api is needed in java?   Hi Friend, The Application Programming Interface (API) is a library... to know more about java api, please visit the following link: Java API Thanks
Java dom from string
Java dom from string  Hi, How to create dom tree from string data? I am trying to search the example for java dom from string. Thanks   Hi, Following code might be useful: import; import
java api
java api  what is work of api in java   An application programming interface (API) is a library of functions that Java provides.... For more information,visit the following link: Java API
java API
java API  hello What is the Java API?   hello, The Java API is a large collection of ready-made software components that provide many useful capabilities, such as graphical user interface (GUI) widgets
Java-XML-DOM  Hi! I need some help. I have to make java program that loads an xml file and from it builds DOM(later i will have to work with it - like using xpath in java find some value and replace it...). Since i'm new to java
jpcap_api  **how can I implement JPCAP API with Java what are the steps needs to do with jdk1.6
DOM paring problem
DOM paring problem  Hi My code always show arrayindexoutofbound here is my code i am using arraylist to display and DOM parser to retrieve XML value. I am searching the value by attributes equals 2. this error i am getting