8 Replies Latest reply: Jan 15, 2009 6:09 PM by 807589 RSS

    Parsing Empty XML entries?

    807589
      I am trying to parse an XML file and I am having trouble dealing with empty fields.

      An example XML File to demonstrate:
      <entries>
      <?xml version="1.0"?>
      <entries>
           <order>
                <orderid>200</orderid>
                <ticket>120292</ticket>
           </order>
           <order>
                <orderid>201</orderid>
                <ticket/>
           </order>
      </entries>
      The problem is whenever I only have one tag for each element, a snippet of my code is as follows:
        
          private boolean XMLReader()
          {   
                      try
                      {           
                      DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                      DocumentBuilder db = dbf.newDocumentBuilder();
                      Document doc = db.parse("https://www.blargh.com");
                      doc.getDocumentElement().normalize();
                      System.out.println("Root element " + doc.getDocumentElement().getNodeName());
                      NodeList itemList = doc.getElementsByTagName("order");
                      for(int i = 0; i < itemList.getLength(); i++)
                      {
                          Node firstNode = itemList.item(i);
                          if(firstNode.getNodeType() == Node.ELEMENT_NODE)
                          {
                              Element firstElement = (Element) firstNode;
                              NodeList orderIDElementList = firstElement.getElementsByTagName("orderid");
                              Element orderIDElement = (Element) orderIDElementList.item(0);
                              NodeList orderID = orderIDElement.getChildNodes();
                              System.out.println("OrderID: " + ((Node) orderID.item(0)).getNodeValue());                  
                              
                              NodeList ticketElementList = firstElement.getElementsByTagName("ticket");
                              Element ticketElement = (Element) ticketElementList.item(0);
                              NodeList ticket = ticketElement.getChildNodes();
                              System.out.println("Ticket: " + ((Node) ticket.item(0)).getNodeValue());  
                          }
                      }
                      return true;
                  } catch (Exception e) {
                      e.printStackTrace();
                      return false;
                  }
          }
      Any suggestions on how to remedy getting a Null Pointer it would be greatly appreciated, I might even need a whole new method of reading the file in. Thanks for the help!
        • 1. Re: Parsing Empty XML entries?
          798635
          nummsk wrote:
          NodeList ticketElementList = firstElement.getElementsByTagName("ticket");
          Element ticketElement = (Element) ticketElementList.item(0);
          You can check for the zero size by using NodeList.getLength() by checking ticketElement against null.
          • 2. Re: Parsing Empty XML entries?
            807589
            I kinda feel silly for asking, but your reply kind of confuses me, how would it be executed?
            NodeList ticketElementList = firstElement.getElementsByTagName("ticket");
                     Element ticketElement = (Element) ticketElementList.item(0);
                     if(ticketElement != null)
                     {
                          ...
                     }
            That doesn't seem to make any sense to me... maybe I missed something.
            • 3. Re: Parsing Empty XML entries?
              798635
              That would get rip of NPE, as you can't reference a non-existing child text node. How are you going to deal with an empty tag is entirely up to you.
              • 4. Re: Parsing Empty XML entries?
                807589
                Im sorry, I still don't understand how this is checking against an empty XML tag, I have tried numerous variations of checking for null, but nothing seems to work. Could I please have further guidance? I tried looking with the debugger and I can't even seem to find where the values are being stored to check against. Ive really hit a brick wall here.
                • 5. Re: Parsing Empty XML entries?
                  DrClap
                  What's this "check for null" business? You seem to have missed the important bit from Reply 1:
                  You can check for the zero size by using NodeList.getLength()...
                  • 6. Re: Parsing Empty XML entries?
                    807589
                    That is precisely what I do not understand.
                    You can check for the zero size by using NodeList.getLength() by checking ticketElement against null.
                    I need to check NodeList.getLength() by checking ticketElement against null? This doesn't make any sense to me, I need to check ticketElement against null then by doing that I can somehow use NodeList.getLength() by checking it against zero? I must be interpreting this wrong.

                    Edited by: nummsk on Jan 15, 2009 3:53 PM
                    • 7. Re: Parsing Empty XML entries?
                      798635
                      Sorry, my reply was missing an OR:

                      ...check NodeList.getLength() or check ticketElement against null...

                      Either one would work.
                      • 8. Re: Parsing Empty XML entries?
                        807589
                        I got it to work now, thanks for the help =). Boy was I confused for a while.