• Post Reply
  • Bookmark Topic Watch Topic
  • New Topic

DOM Parsing-Very Very Basic Doubt

 
pradeep chellappan
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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
Sheriff
Posts: 21322
32
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
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".
 
  • Post Reply
  • Bookmark Topic Watch Topic
  • New Topic