File APIs for Java Developers
Manipulate DOC, XLS, PPT, PDF and many others from your application.
The moose likes XML and Related Technologies and the fly likes DOM Parsing-Very Very Basic Doubt Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "DOM Parsing-Very Very Basic Doubt " Watch "DOM Parsing-Very Very Basic Doubt " New topic

DOM Parsing-Very Very Basic Doubt

pradeep chellappan

Joined: Oct 20, 2005
Posts: 29
Hi All,

I am Beginner in XML parsing world.

Here is my Xml...

<?xml version="1.0" encoding="ISO-8859-1"?>

<MainFolder name="Projectname">

Here is the program i tried to attempt:


import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import javax.xml.parsers.ParserConfigurationException;

import org.w3c.dom.Document;
import org.w3c.dom.Element;
import org.w3c.dom.NodeList;
import org.xml.sax.SAXException;

public class ParseTrial {
public static Document dom ;

public static void main(String args[]){

public static void parseXml(){
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance() ;
try {
DocumentBuilder db = dbf.newDocumentBuilder() ;
try {
dom = db.parse("d:\\sample.xml") ;

} catch (SAXException e1) {
} catch (IOException e1) {
} catch (ParserConfigurationException e) {
public static void parseDocument(){
Element root = dom.getDocumentElement() ;

NodeList nList = root.getElementsByTagName("MainFolder") ;

int nlistCount = nList.getLength() ;

System.out.println("Nodelist length:"+ nlistCount);

I am getting the output as "Nodelist length:0"

I was expecting the output as 2 since there are two elements under the element "Mainfolder".

Can somebody please clarify.

Advance Thanks.
Paul Clapham

Joined: Oct 14, 2005
Posts: 19973

Originally posted by pradeep chellappan:
I was expecting the output as 2 since there are two elements under the element "Mainfolder".
But you wrote code that asked for a list of elements which are descendants of the root and whose name is "MainFolder" (not "Mainfolder"). There are no such elements. The root does indeed have two element children (and three text children, by the way) but neither of them is named "MainFolder".
I agree. Here's the link:
subject: DOM Parsing-Very Very Basic Doubt
It's not a secret anymore!