Results 1 to 6 of 6
  1. #1
    gayathri_g is offline Member
    Join Date
    Aug 2009
    Posts
    9
    Rep Power
    0

    Default 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...???

    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>
    [/CODE]

  2. #2
    r035198x is offline Senior Member
    Join Date
    Aug 2009
    Posts
    2,388
    Rep Power
    8

    Default

    What was your expected output and what output did you get?

  3. #3
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,967
    Rep Power
    19

    Default

    Quote Originally Posted by gayathri_g View Post
    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...???
    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>:

    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>
    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>).

    ETA: since, presumably, you want to count the classrooms for a date then just select them, rather than all child nodes.

  4. #4
    gayathri_g is offline Member
    Join Date
    Aug 2009
    Posts
    9
    Rep Power
    0

    Default

    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.

  5. #5
    gayathri_g is offline Member
    Join Date
    Aug 2009
    Posts
    9
    Rep Power
    0

    Default

    since, presumably, you want to count the classrooms for a date then just select them, rather than all child nodes.
    how to select only the classroom node under each "date" node?

  6. #6
    Tolls is offline Moderator
    Join Date
    Apr 2009
    Posts
    11,967
    Rep Power
    19

    Default

    Quote Originally Posted by gayathri_g View Post
    how to select only the classroom node under each "date" node?
    Your date Node (ie the Nodes in your original NodeList) is an Element. So cast it to Element and use the methods in Element (in fact getElementsByTagName() that you've already used for the document) to get the classrooms.

Similar Threads

  1. Replies: 5
    Last Post: 08-21-2009, 11:03 AM
  2. Image reading from URL problem
    By paras in forum Advanced Java
    Replies: 4
    Last Post: 04-18-2009, 07:26 AM
  3. [SOLVED] Image reading from URL problem
    By paras in forum New To Java
    Replies: 3
    Last Post: 04-18-2009, 05:11 AM
  4. reading textfile from java problem
    By saytri in forum New To Java
    Replies: 1
    Last Post: 01-17-2008, 02:13 AM
  5. Problem reading an xml file with AJAX
    By Fiona80 in forum XML
    Replies: 0
    Last Post: 12-17-2007, 08:02 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •