getNodeValue returning null from xml DOM


Last Updated:

  1. GordonEndersby

    GordonEndersby Active Member This Topic's Starter

    Joined:
    Dec 15, 2009
    Messages:
    39
    Likes Received:
    1
    Dear all,

    Im having problems with a bit of code I knocked up in Java outside of the android environment. As part of a console application it worked fine.
    But now Im using it in a Widget Im trying to knock up its not working as I expect it to. The widget is compiling and working on the emulator.
    The xml is very short and simple as its generated by an Arduino microcontroler running as a web server with a couple of temperature sensors and an ethernet board.
    This is my first stab at Java but have plenty of other programming experience as a web developer in Microsoft world with C# and scripting languages. Ive been banging my head for the last few hours and have probably missed something obvious.

    I create the DOM like this:
    Code (Text):
    1.             URL url = new URL("http://xxxxxx.xxx");
    2.             DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
    3.             DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
    4.             Document doc = dBuilder.parse(new InputSource(url.openStream()));
    5.             doc.getDocumentElement().normalize();
    6.  
    The XML looks like this:
    Code (Text):
    1. <temp>
    2. <time>18:33 - 14 1 2010</time>
    3. <int>19.12</int>
    4. <ext>3.06</ext>
    5. </temp>
    I then call:
    text += getTagValue("time", doc);
    To append the value returned to a text string.

    Im my console application it returns the text from between the <time> </time> tags. But in the widget it returns null.

    Code (Text):
    1.     private static String getTagValue(String tag, Document doc) {
    2.         Element intElement = (Element) doc.getElementsByTagName(tag).item(0);
    3.         return intElement.getNodeValue();
    4.     }
    Is there some difference in the java implementation within the Android version that is causing this?
    Or do I need to cast the NodeValue or the returned String in some way?

    Thanks

    Gordon
     

    Advertisement
  2. KeithG

    KeithG Well-Known Member

    Joined:
    Nov 15, 2009
    Messages:
    103
    Likes Received:
    3
    Use the debugger... would be my first look.

    Set the breakpoint at the return, and inspect the intElement object. If that doesn't provide any illumination.

    undo the method chaining into separate discrete vars and step through checking to make sure your getting what you think you should.
     
  3. GordonEndersby

    GordonEndersby Active Member This Topic's Starter

    Joined:
    Dec 15, 2009
    Messages:
    39
    Likes Received:
    1
    I havnt managed to get the debugger working yet, so have been using Toast popups to show whats happening. Not only does the widget text element show null but also the popups ive show using this routine.

    Code (Text):
    1.     private static void PopUp (CharSequence text, Context context, int duration){
    2.         Toast toast = Toast.makeText(context, text, duration);
    3.         toast.show();    
    4.     }
    5.  
    Gordon
     
  4. GordonEndersby

    GordonEndersby Active Member This Topic's Starter

    Joined:
    Dec 15, 2009
    Messages:
    39
    Likes Received:
    1
    Debbuger is working on a normal android application but not for widgets.
    Ill try hacking my code into a non widget application and see if I can work out what is happening from there.

    Gordon
     
  5. markb

    markb Well-Known Member

    Joined:
    Sep 14, 2009
    Messages:
    148
    Likes Received:
    26
    Hi Gordon,

    I tried your code out in the debugger.
    There seems to be an additional level that you have to take into consideration.

    This works for me.

    Code (Text):
    1.  
    2.     private static String getTagValue(String tag, Document doc) {
    3.         Element intElement = (Element) doc.getElementsByTagName(tag).item(0);
    4. //        return intElement.getNodeValue();
    5.         return intElement.getChildNodes().item(0).getNodeValue() ;
    6.     }
    7.  
    I'm not sure why it works for you in a console app, but not as an Android app.
    It could be a bug in the Android implementation (or different interpretation of the spec).

    Mark
     
  6. GordonEndersby

    GordonEndersby Active Member This Topic's Starter

    Joined:
    Dec 15, 2009
    Messages:
    39
    Likes Received:
    1
    Thanks I havnt had a chance yet to try it outside of a widget.
    I appreciate you trying it for me.
    It looks like its not possible for me to debug a widget through Eclipse.

    Your code suggests to me that I shouldnt be using an Element for my match from doc.getElementsByTagName(tag).item(0)

    There must be a Node object of some kind that I can then get the value from as the Element seems to hold a collection of Nodes which is why you have then had to drill down through the Nodes in the element to item(0).

    Ill have a go again tomorrow working from your hint.

    Thanks again.

    Gordon
     
  7. markb

    markb Well-Known Member

    Joined:
    Sep 14, 2009
    Messages:
    148
    Likes Received:
    26
    Hi Gordon,

    You'll already be aware of this I'm sure, but here's where I think the extra level is coming from.

    Code (Text):
    1.  
    2.         Element intElement = (Element) doc.getElementsByTagName(tag).item(0);
    3.  
    getElementsByTagName() doesn't look up a single node, it returns an array of all the nodes in the document. So if you happened to have 3 "time" nodes, then they'd all be returned in the array.

    So...
    Code (Text):
    1.  
    2.         getElementsByTagName(tag).item(0);
    3.  
    ...is giving you your 1st "time" node (in this particular case).

    You then have to go "inside" that time node to get the body text. And the body text is the value of the first child of the time element.

    Or, to say the same thing more explicitly in code:

    Code (Text):
    1.  
    2.     private static String getTagValue(String tag, Document doc) {
    3.         Element firstElementWithName = (Element) doc.getElementsByTagName(tag).item(0);
    4.         Node firstChild = firstElementWithName.getFirstChild() ;
    5.         String value = firstChild.getNodeValue() ;
    6.        
    7.         return value ;
    8.     }
    9.  
    Mark
     
  8. GordonEndersby

    GordonEndersby Active Member This Topic's Starter

    Joined:
    Dec 15, 2009
    Messages:
    39
    Likes Received:
    1
    Thanks guys,
    Its a lot easier with the debugger to work through this.
    I did a bit of searching last night and couldnt find anything about problems debugging widgets with Eclipse. Ill keep looking though.
    In the meantime Ill work on what I can as a regular app them move it to the widget once its working. Throwing in the odd popup toaster to debug in the widget or throw remarks into the device log and view that.

    Thanks again

    Gordon
     

Share This Page

Loading...