• After 15+ years, we've made a big change: Android Forums is now Early Bird Club. Learn more here.

Apps getNodeValue returning null from xml DOM

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:
            URL url = new URL("http://xxxxxx.xxx");
            DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance();
            DocumentBuilder dBuilder = dbFactory.newDocumentBuilder();
            Document doc = dBuilder.parse(new InputSource(url.openStream()));
            doc.getDocumentElement().normalize();
The XML looks like this:
Code:
<temp>
<time>18:33 - 14 1 2010</time>
<int>19.12</int>
<ext>3.06</ext>
</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:
    private static String getTagValue(String tag, Document doc) {
        Element intElement = (Element) doc.getElementsByTagName(tag).item(0);
        return intElement.getNodeValue();
    }
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
 
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.
 
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:
    private static void PopUp (CharSequence text, Context context, int duration){
        Toast toast = Toast.makeText(context, text, duration);
        toast.show();    
    }
Gordon
 
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
 
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:
    private static String getTagValue(String tag, Document doc) {
        Element intElement = (Element) doc.getElementsByTagName(tag).item(0);
//        return intElement.getNodeValue();
        return intElement.getChildNodes().item(0).getNodeValue() ;
    }

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
 
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
 
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:
        Element intElement = (Element) doc.getElementsByTagName(tag).item(0);

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:
        getElementsByTagName(tag).item(0);
...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:
    private static String getTagValue(String tag, Document doc) {
        Element firstElementWithName = (Element) doc.getElementsByTagName(tag).item(0);
        Node firstChild = firstElementWithName.getFirstChild() ;
        String value = firstChild.getNodeValue() ;
        
        return value ;
    }

Mark
 
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
 
Back
Top Bottom