BlackBerry Forums Support Community
              

Closed Thread
 
Thread Tools
Old 07-18-2008, 02:20 PM   #1
christiand
New Member
 
Join Date: Jun 2008
Model: none
PIN: N/A
Carrier: None
Posts: 4
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
richard.puckett
Talking BlackBerry Encyclopedia
 
richard.puckett's Avatar
 
Join Date: Oct 2007
Location: Seattle, WA
Model: 9020
PIN: N/A
Carrier: T-Mobile
Posts: 212
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

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


HealthKit 4802 Computer Oscilloscope Heath Computer Systems picture

HealthKit 4802 Computer Oscilloscope Heath Computer Systems

$120.00



Computer Boards CIO-DAS1602/16 DAQ Card, 8-bit ISA, 16-Ch 16-Bit 100Khz with I/O picture

Computer Boards CIO-DAS1602/16 DAQ Card, 8-bit ISA, 16-Ch 16-Bit 100Khz with I/O

$600.00



SCHNEIDER Twido TW2AMI2HT Analog 2 in 0-10v, 4-20ma Module PLC Modicon picture

SCHNEIDER Twido TW2AMI2HT Analog 2 in 0-10v, 4-20ma Module PLC Modicon

$119.99



Schneider PLC TWIDO TM2AMM6HT ANALOGUE I/O MODULE 20mA 24VDC Missing Terminal picture

Schneider PLC TWIDO TM2AMM6HT ANALOGUE I/O MODULE 20mA 24VDC Missing Terminal

$156.00



SCHNEIDER Twido TWDAMI2HT Analog 2 in 0-10v, 4-20ma Module PLC Modicon picture

SCHNEIDER Twido TWDAMI2HT Analog 2 in 0-10v, 4-20ma Module PLC Modicon

$119.99



Measurement Computing 197728B USB 1608HS-2A0 16-Channel Digital DAQ +2 Analog picture

Measurement Computing 197728B USB 1608HS-2A0 16-Channel Digital DAQ +2 Analog

$487.50







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