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
PIN: N/A
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..)

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

OutputStream os = conn.openOutputStream();
os.write(encodedData.getBytes());
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?

edit:
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,

Christian

Last edited by christiand : 07-18-2008 at 03:08 PM.
Offline  
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
PIN: N/A
Carrier: T-Mobile
Posts: 212
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

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"

Code:
	
<connection id="7" type="WAP1" description="T-Mobile (USA)">
  <params>
    <param name="WapGatewayAPN" value="wap.voicestream.com"/>
    <param name="WapGatewayIP" value="216.155.165.50"/>
  </params>
</connection>
would be parsed by:

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

        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);
                
                connectionExtensions.addElement(connectionExtension);
            }
        }

        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.
Offline  
Closed Thread


Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On





Copyright 2004-2014 BlackBerryForums.com.
The names RIM and BlackBerry are registered Trademarks of BlackBerry Inc.