Results 1 to 6 of 6
Thread: Problem in reading xml
- 08-27-2009, 02:17 PM #1
Member
- Join Date
- Aug 2009
- Posts
- 9
- Rep Power
- 0
Problem in reading xml
i tried to get the number childnodes from a tag in a xml.But its showing even the nodes inside the child nodes.Can anyone correct me...???
[/CODE]Java Code:package edc; import java.io.IOException; import java.util.logging.Level; import java.util.logging.Logger; import javax.xml.parsers.DocumentBuilder; import javax.xml.parsers.DocumentBuilderFactory; import javax.xml.parsers.ParserConfigurationException; import org.w3c.dom.Document; import org.w3c.dom.NodeList; import org.w3c.dom.NodeList; import org.w3c.dom.NodeList; import org.xml.sax.SAXException; public class ReadXML { NodeList nodelist; public static void main(String [] args) { ReadXML obj = new ReadXML(); obj.parse(); } public void parse() { try { DocumentBuilderFactory docfact = DocumentBuilderFactory.newInstance(); DocumentBuilder docbuilder = docfact.newDocumentBuilder(); Document doc; doc = docbuilder.parse("d:/files/edc.xml"); nodelist = (NodeList) doc.getElementsByTagName("date"); int dateNodecount = nodelist.getLength(); for(int i=0;i<dateNodecount;i++) { int count = nodelist.item(i).getChildNodes().getLength();//getNodeName(); System.out.println(count); } } catch (SAXException ex) { Logger.getLogger(ReadXML.class.getName()).log(Level.SEVERE, null, ex); } catch (IOException ex) { Logger.getLogger(ReadXML.class.getName()).log(Level.SEVERE, null, ex); } catch (ParserConfigurationException ex) { Logger.getLogger(ReadXML.class.getName()).log(Level.SEVERE, null, ex); } } } here is my xml: [CODE] <edc> <date value = "28-8-2009"> <classroom value = "c1"> <session id = "3" language = "english" lic_type = "l.v" lesson_code = "l1" st_cnt = "12" staff_id = "staff-01"> </session> </classroom> <classroom value = "c1"> <session id = "4" language = "english" lic_type = "l.v" lesson_code = "l2" st_cnt = "12" staff_id = "staff-01"/> </classroom> <classroom value = "c2"> <session id = "1" language = "urudu" lic_type = "l.v" lesson_code = "l1" st_cnt = "17" staff_id = "staff-02"/> </classroom> <classroom value = "c2"> <session id = "2" language = "urudu" lic_type = "l.v" lesson_code = "l2" st_cnt = "17" staff_id = "staff-02"/> </classroom> </date> <date value = "29-8-2009"> <classroom value = "c1"> <session id = "3" language = "english" lic_type = "l.v" lesson_code = "l3" st_cnt = "12" staff_id = "staff-01"/> </classroom> <classroom value = "c1"> <session id = "4" language = "english" lic_type = "l.v" lesson_code = "l4" st_cnt = "12" staff_id = "staff-01"/> </classroom> <classroom value = "c2"> <session id = "1" language = "urudu" lic_type = "l.v" lesson_code = "l3" st_cnt = "17" staff_id = "staff-02"/> </classroom> <classroom value = "c2"> <session id = "2" language = "urudu" lic_type = "l.v" lesson_code = "l4" st_cnt = "17" staff_id = "staff-02"/> </classroom> </date> </edc>
- 08-27-2009, 02:58 PM #2
Senior Member
- Join Date
- Aug 2009
- Posts
- 2,388
- Rep Power
- 6
What was your expected output and what output did you get?
- 08-27-2009, 03:26 PM #3
Moderator
- Join Date
- Apr 2009
- Posts
- 10,468
- Rep Power
- 16
It's not showing the nodes inside the children, it's showing the text nodes. All that whitespace is text, to the parser, and is treated as a node.
So this <date>:
has a text node (the new line after <date> and before <classroom>), an element node (the classroom), and another text node (the new line after </classroom> and before </date>).Java Code:<date value = "28-8-2009"> <classroom value = "c1"> <session id = "3" language = "english" lic_type = "l.v" lesson_code = "l1" st_cnt = "12" staff_id = "staff-01"> </session> </classroom> </date>
ETA: since, presumably, you want to count the classrooms for a date then just select them, rather than all child nodes.
- 08-28-2009, 05:56 AM #4
Member
- Join Date
- Aug 2009
- Posts
- 9
- Rep Power
- 0
my expected o/p ,for the first tag(date) the count of childnode(classroom) should be 4 and for the count should be 4 but i'm getting 9 and 9.
- 08-28-2009, 05:57 AM #5
Member
- Join Date
- Aug 2009
- Posts
- 9
- Rep Power
- 0
how to select only the classroom node under each "date" node?since, presumably, you want to count the classrooms for a date then just select them, rather than all child nodes.
- 08-28-2009, 09:10 AM #6
Moderator
- Join Date
- Apr 2009
- Posts
- 10,468
- Rep Power
- 16
Similar Threads
-
problem with reading excel sheet data reading using poi libraries
By sandeepsai17 in forum New To JavaReplies: 5Last Post: 08-21-2009, 11:03 AM -
Image reading from URL problem
By paras in forum Advanced JavaReplies: 4Last Post: 04-18-2009, 07:26 AM -
[SOLVED] Image reading from URL problem
By paras in forum New To JavaReplies: 3Last Post: 04-18-2009, 05:11 AM -
reading textfile from java problem
By saytri in forum New To JavaReplies: 1Last Post: 01-17-2008, 02:13 AM -
Problem reading an xml file with AJAX
By Fiona80 in forum XMLReplies: 0Last Post: 12-17-2007, 08:02 PM


LinkBack URL
About LinkBacks


Bookmarks