This week's giveaway is in the Android forum.
We're giving away four copies of Android Security Essentials Live Lessons and have Godfrey Nolan on-line!
See this thread for details.
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


Win a copy of Android Security Essentials Live Lessons this week in the Android forum!
JavaRanch » Java Forums » Engineering » XML and Related Technologies
Bookmark "DOM Parsing-Very Very Basic Doubt " Watch "DOM Parsing-Very Very Basic Doubt " New topic
Author

DOM Parsing-Very Very Basic Doubt

pradeep chellappan
Greenhorn

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

I am Beginner in XML parsing world.

Here is my Xml...

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

<MainFolder name="Projectname">
<SubFolder1>Br</SubFolder1>
<SubFolder2>DD</SubFolder2>
</MainFolder>

Here is the program i tried to attempt:

import java.io.IOException;

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[]){
parseXml();
parseDocument();
}

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

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

NodeList nList = root.getElementsByTagName("MainFolder") ;
System.out.println(nList.toString());

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
Bartender

Joined: Oct 14, 2005
Posts: 18541
    
    8

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".
 
It is sorta covered in the JavaRanch Style Guide.
 
subject: DOM Parsing-Very Very Basic Doubt
 
Similar Threads
Display contents in GUI format
help with web service to display xml file contents using netbeans
Unable to get the correct data from the XML file
servlets and xml
Xinclude, read 2 xml files