• Post Reply Bookmark Topic Watch Topic
  • New Topic
programming forums Java Mobile Certification Databases Caching Books Engineering Micro Controllers OS Languages Paradigms IDEs Build Tools Frameworks Application Servers Open Source This Site Careers Other Pie Elite all forums
this forum made possible by our volunteer staff, including ...
Marshals:
  • Campbell Ritchie
  • Jeanne Boyarsky
  • Ron McLeod
  • Paul Clapham
  • Liutauras Vilda
Sheriffs:
  • paul wheaton
  • Rob Spoor
  • Devaka Cooray
Saloon Keepers:
  • Stephan van Hulst
  • Tim Holloway
  • Carey Brown
  • Frits Walraven
  • Tim Moores
Bartenders:
  • Mikalai Zaikin

DOM Parsing-Very Very Basic Doubt

 
Greenhorn
Posts: 29
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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.
 
Marshal
Posts: 28226
95
Eclipse IDE Firefox Browser MySQL Database
  • Mark post as helpful
  • send pies
    Number of slices to send:
    Optional 'thank-you' note:
  • 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".
reply
    Bookmark Topic Watch Topic
  • New Topic