BlackBerry Forums Support Community

Closed Thread
LinkBack Thread Tools
Old 10-14-2009, 05:36 AM   #1 (permalink)
New Member
Join Date: Oct 2009
Model: 8900
Carrier: Vodaphone
Posts: 11
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default accessing xml from a server

Please Login to Remove!

Hey guys I have had a play with the Sample xml reader program but I want to be able to access a remote xml and it just doesnt seem to want to do it. Has anybody done this if so what should i be doing??

Old 10-14-2009, 11:24 AM   #2 (permalink)
BlackBerry Extraordinaire
Join Date: Jan 2006
Model: LEZ10
OS: 10.0.10
Carrier: Rogers CA
Posts: 1,704
Post Thanks: 20
Thanked 77 Times in 68 Posts

Well that depends on so many things, like: what kind of server; and many many more.

I use an HttpConnection to get XML from a web server, like Apache. I've also used a socket connection via MDS on a BES to connect to a purpose built server that provides XML data.
My other Blackberry is a PlayBook.
Old 10-16-2009, 04:47 AM   #3 (permalink)
New Member
Join Date: Oct 2009
Model: 8900
Carrier: Vodaphone
Posts: 11
Post Thanks: 0
Thanked 0 Times in 0 Posts

well I have been given some code which "apparently" works....

import net.rim.device.api.ui.*;
import net.rim.device.api.ui.component.*;
import net.rim.device.api.ui.container.*;
import net.rim.device.api.xml.parsers.*;
import org.w3c.dom.*;

class XML_Parsing_Sample extends UiApplication{
    //creating a member variable for the MainScreen
    MainScreen _screen= new MainScreen();
    //string variables to store the values of the XML document
    String _node,_element,_pLength;
    Connection _connectionthread;

    public static void main(String arg[]){
        XML_Parsing_Sample application = new XML_Parsing_Sample();
        //create a new instance of the application
        //and start the application on the event thread

    public XML_Parsing_Sample() {
        _screen.setTitle("XML Parsing");//setting title
        _screen.add(new RichTextField("Requesting....."));
        _screen.add(new RichTextField("Sucker.."));
        _screen.add(new SeparatorField());
        pushScreen(_screen); // creating a screen
        //creating a connection thread to run in the background
        _connectionthread = new Connection();
        _connectionthread.start();//starting the thread operation

    public void updateField(String node, String element, String pLength){
        //receiving the parsed node and its value from the thread
        //and updating it here
        //so it can be displayed on the screen
        _screen.add(new RichTextField(pLength));
        String title="Book";
        _screen.add(new RichTextField(node+" : "+element));

            _screen.add(new SeparatorField());

    private class Connection extends Thread{
        public Connection(){

        public void run(){
            // define variables later used for parsing
            //Document doc;
            StreamConnection conn;

                //providing the location of the XML file,
                //your address might be different
                //next few lines creates variables to open a
                //stream, parse it, collect XML data and
                //extract the data which is required.
                //In this case they are elements,
                //node and the values of an element
                DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
                DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
                InputStream inputStream = conn.openInputStream();
                Document doc = docBuilder.parse(inputStream);
                //DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory. newInstance(); 
                //DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
                //doc = docBuilder.parse(conn.openInputStream());
                doc.getDocumentElement ().normalize ();
                NodeList listOfProperty = doc.getElementsByTagName("Title");
                int totalProperties = listOfProperty.getLength();
                System.out.println("Total no of properties : " + totalProperties);
                //initiate new vars
                _node=new String();
                _element = new String();
                _pLength = new String();
                //this "for" loop is used to parse through the
                //XML document and extract all elements and their
                //value, so they can be displayed on the device

                for (int i=0;i<listOfProperty.getLength();i++)
                        Node value=listOfProperty.item(i).getChildNodes().item(0);
                        _pLength = Integer.toString(listOfProperty.getLength());
                        System.out.println(_node +":"+_element);
                }//end for
            }//end try
            //will catch any exception thrown by the XML parser
            catch (Exception e){
        }//end connection function
    }// end connection class
}//end XML_Parsing_Sample
I have messed around with it a bit trying to get it to do something but no joy.

I already have some code that parses an xml feed (none BB dev) which works fine so I know the theory is correct.

Copyright 2004-2016
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.