Results 1 to 5 of 5
  1. #1
    alley is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

    Default XML parsing using DOM

    Hello,

    I'm need to extract the contents of, say, the <Date><\Date> tags.

    I'm using this sample code, but when the program runs nothing happens, there's no actual output.

    Java Code:
    import java.io.*;
    import javax.xml.parsers.*;
    import org.w3c.dom.*;
    
    public class XMLParse
     {         
         public void test() 
          {
             try
              {
                DocumentBuilder docbuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
                Document doc = docbuilder.parse(new File("C:\\Java\\TestFile.xml"));            
                Element qu = doc.getElementById("Date");
                doc.getDocumentElement().normalize();            
                System.out.println(qu.getTextContent());
              }
             catch(Exception e)
             {}
          } 
      }
    Can you please suggest what could be wrong?

    Thanks for your help!

  2. #2
    Fubarable's Avatar
    Fubarable is offline Moderator
    Join Date
    Jun 2008
    Posts
    19,316
    Blog Entries
    1
    Rep Power
    26

  3. #3
    alley is offline Member
    Join Date
    Nov 2007
    Posts
    8
    Rep Power
    0

    Default

    Hi Fubarable,

    Yes I know about that, I would elaborate further on the code in regards to exceptions, this is just a test program to check about parsing.

    Thanks.

  4. #4
    BeMathis is offline Member
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Default

    The reason Fubarable mentioned your Exception is that there is a possibility that an exception is being caught and you are not seeing anything because of it. Basic try catch procedure suggests you at least have some type of console output inside the structure of catch(Exception e){}

    Remove:
    Java Code:
    catch(Exception e){}
    ADD:
    Java Code:
    catch(Exception e){e.printStackTrace();}
    This will better aid you in future problems and could have helped you avoid this situation altogether. There may be a null pointer exception.

    Now, in regards to your problem.
    Best practice it to use xpath when you are extracting elements from an XML. Inherent nature of a DOM tree is unintuitive and can give rise to strange problems where you grab empty text nodes if your XML has strange formatting.
    If you do not like xpath:
    Then you are in for a wold of pain imho :D try qu.getNodeValue() or qu.getNodeName() to make sure that you are actually getting the <Date> element. I will run your code and see what I get.
    Last edited by BeMathis; 10-14-2009 at 06:04 PM.

  5. #5
    BeMathis is offline Member
    Join Date
    Oct 2009
    Posts
    4
    Rep Power
    0

    Default

    try this. its a little dirty but it may be a step in the right direction

    Java Code:
    try{
        DocumentBuilder docbuilder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
        Document doc = docbuilder.parse(new File("C:\\Java\\TestFile.xml"));
        NodeList qu = (NodeList) root.getElementsByTagName("standard_data_type");
        for(int x=0; x<qu.getLength(); x++){
    	System.out.println(qu.item(x).getNodeName());
    	qu.item(x).normalize();
    	System.out.println(qu.item(x).getTextContent());
        }
    }catch(Exception e)
     {e.printStackTrace();}
    Last edited by BeMathis; 10-14-2009 at 06:19 PM.

Similar Threads

  1. Parsing XML
    By virvalid in forum Advanced Java
    Replies: 3
    Last Post: 08-10-2009, 12:40 PM
  2. Parsing
    By Matt Sakko in forum New To Java
    Replies: 14
    Last Post: 03-21-2009, 04:49 PM
  3. Xml Parsing
    By Nomad in forum XML
    Replies: 12
    Last Post: 02-22-2009, 11:19 AM
  4. xml parsing help
    By tankhardrive in forum XML
    Replies: 2
    Last Post: 02-11-2009, 11:45 PM
  5. Parsing URL
    By Java Tip in forum Java Tip
    Replies: 0
    Last Post: 12-26-2007, 10:16 AM

Posting Permissions

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