Welcome!

Welcome to the official BlackBerry Support Community Forums.

This is your resource to discuss support topics with your peers, and learn from each other.

inside custom component

Java Development

Reply
New Contributor
Posts: 6
Registered: ‎01-18-2011
My Device: 9630
My Carrier: Blackberry

How to read XML over HTTP

Hello,

 

I have a simple XML file over http.

 

I have to read the data from XML file through the blackberry code.

 

I am using blackberry api 6.

 

in XML files some JPG URLs are also there.

 

Could you let me know how can i read the XML to display the data and images on device?

If possible provide an example.

 

it's an urgent requirement.

Thanks in advance

 

Rocky

 

 

 

 

Developer
Posts: 17,012
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: How to read XML over HTTP

welcome to the support forum.

you have two options at first:
download the whole file first, or use a pull parser.
i use the second option. you can use the parser from the API:
http://www.blackberry.com/developers/docs/6.0.0api/net/rim/device/api/xml/jaxp/XMLParser.html#parse(... org.xml.sax.helpers.DefaultHandler)

the inputstream can be taken directly from the httpconnection after checking the status code.
personally i like to use kxml, a parser from a library, because it is faster than the one in the API.

to download the pictures you'll have to extract the URLs and start a separate download for them.
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
New Contributor
Posts: 6
Registered: ‎01-18-2011
My Device: 9630
My Carrier: Blackberry

Re: How to read XML over HTTP

Thank you so much for your reply.

 

If its not a big thing, Could you provide me an example.

 

Rocky

Developer
Posts: 17,012
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: How to read XML over HTTP

check the xml demo in your eclipse samples folder
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
New Contributor
Posts: 6
Registered: ‎01-18-2011
My Device: 9630
My Carrier: Blackberry

Re: How to read XML over HTTP

Yes, I check that example.

 

But it is reading the XML which in our system. Can't we read XML over the HTTP directly.

 

Rocky

Developer
Posts: 17,012
Registered: ‎07-29-2008
My Device: Z10 LE, Z30, Passport
My Carrier: O2 Germany

Re: How to read XML over HTTP

yes, as i said:
http://www.blackberry.com/developers/docs/6.0.0api/javax/microedition/io/HttpConnection.html
in special
http://www.blackberry.com/developers/docs/6.0.0api/javax/microedition/io/InputConnection.html#openIn...
----------------------------------------------------------
feel free to press the like button on the right side to thank the user that helped you.
please mark posts as solved if you found a solution.
@SimonHain on twitter
Developer
Posts: 19,636
Registered: ‎07-14-2008
My Device: Not Specified

Re: How to read XML over HTTP

Just to prove that different developers do things different ways, I download the whole XML before I parse it.  The httpDemo which you should find with your tooling also downloads the entire 'page', so this should provide you with a demo of that part. 

New Contributor
Posts: 6
Registered: ‎01-18-2011
My Device: 9630
My Carrier: Blackberry

Re: How to read XML over HTTP

class Connection extends Thread
{
    String _node, _element;

    public Connection()
    {
        super();
    }

    public void run(String url)
    {
        // define variables later used for parsing
        Document doc;
        StreamConnection conn;
        try
        {
            // providing the location of the XML file,
            // your address might be different
            conn = (StreamConnection) Connector.open(url);
            // 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();
            docBuilder.isValidating();
            doc = docBuilder.parse(conn.openInputStream());
            doc.getDocumentElement().normalize();
            NodeList list = doc.getElementsByTagName("*");
            _node = new String();
            _element = new String();
            // get the event lock to avoid IllegelStateException while
            // accessing the UI
            Object obj = Application.getEventLock();
            // 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 < list.getLength(); i++)
            {
                synchronized (obj)
                {
                    Node value = list.item(i).getChildNodes().item(0);
                    _node = list.item(i).getNodeName();
                    _element = value.getNodeValue();

                    Dialog.alert(_node);

                    Dialog.alert(_element);
                }
            }// end for
        }// end try
            // will catch any exception thrown by the XML parser
        catch (Exception e)
        {
            Dialog.alert(e.toString());
        }
    }// end connection function inside run
}// end connection class

-----------------------------------------------------------------------------------------------------

pass the url as follows...

 

 

Connection obj = new Connection();
        obj.run("<URL>;deviceside=true");