aspose file tools*
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 Spring in Action this week in the Spring 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: 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">
<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: 18669
    
    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".
 
I agree. Here's the link: http://aspose.com/file-tools
 
subject: DOM Parsing-Very Very Basic Doubt