BlackBerry Forums Support Community

Closed Thread
LinkBack Thread Tools
Old 07-18-2008, 02:20 PM   #1 (permalink)
New Member
Join Date: Jun 2008
Model: none
Carrier: None
Posts: 4
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default parsing/manipulating XML

Please Login to Remove!

My application is to request (HTTP POST) an xml file from a website, and then format the data in the response on screen.

I'm very new to blackberry programming, and similarly new to java, and the API seems short on a lot of details.

I tried to use kXML, but it wouldn't verify properly, and then I found that blackberry has an API for parsing, so it's easier to just use that.

So far... (you can safely assume i've declared the neccessary variables/instantiated the objects..)

conn = (HttpConnection) url );
conn.setRequestMethod( HttpConnection.POST );
conn.setRequestProperty( "Content-Type", type );

OutputStream os = conn.openOutputStream();
InputStream is = conn.openInputStream();
XMLParser parser = new XMLParser();
DefaultHandler handler = new DefaultHandler();
parser.parse(is, handler);
I'm not sure if this works, because I am unaware of what to do next.
How do I manipulate the xml data?
Also how would I test whether or not my http request returned an actual xml file?

After reading a lot of forum posts over again, perhaps I don't understand what the parser preforms, and the role of the handler?
Can anyone shed some light?

Any solutions are appreciated.
Thanks in advance,


Last edited by christiand : 07-18-2008 at 03:08 PM.
Old 07-18-2008, 03:40 PM   #2 (permalink)
Talking BlackBerry Encyclopedia
richard.puckett's Avatar
Join Date: Oct 2007
Location: Seattle, WA
Model: 9020
Carrier: T-Mobile
Posts: 212
Post Thanks: 0
Thanked 0 Times in 0 Posts

What you want to do is override DefaultHandler with your own implementation. Here's a quick example to give you an idea. You just override whatever methods you need from DefaultHandler; generally "startElement", "endElement", and "characters"

<connection id="7" type="WAP1" description="T-Mobile (USA)">
    <param name="WapGatewayAPN" value=""/>
    <param name="WapGatewayIP" value=""/>
would be parsed by:

    private class MyContentHandler extends DefaultHandler {
        private String id;
        private String type;
        private String description;
        private Hashtable params;
        public MyContentHandler() {

        public void endElement(String uri, String localName, String qName) {
            if (localName.equals("connection")) {
                if (id == null) return;
                if (type == null) return;
                ConnectionExtension connectionExtension = new ConnectionExtension(id, type, description, params);

        public void startElement(String uri, String localName, String qName, Attributes attrs) {
            if (localName.equals("connection")) {
                id = attrs.getValue("id");
                type = attrs.getValue("type");
                description = attrs.getValue("description");
                params = new Hashtable();
            else if (localName.equals("param")) {
                String name = attrs.getValue("name");
                String value = attrs.getValue("value");
                params.put(name, value);
Do your homework and know how to ask a good question.

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