BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 07-11-2007, 11:24 AM   #1 (permalink)
New Member
 
Join Date: Jul 2007
Model: 7100
PIN: N/A
Carrier: TMobile
Posts: 13
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default Converting XML

Please Login to Remove!

Good morning,

I'm very new to J2ME programming and definately the first time working on blackberry's JDE. I was wandering if anyone could help me with the following code. I'm trying to output the XML that's received to a readible form and then show it on a field....


Here's the code
Code:
private void parseResults( String xml ){
        
        SAXParserImpl saxparser = new SAXParserImpl();
        ResponseHandler handler = new ResponseHandler();
        ByteArrayInputStream stream = new ByteArrayInputStream(xml.getBytes());
        try { 
            saxparser.parse( stream, handler );
        } catch ( Exception e ) {
            e.printStackTrace();
            response.setText( "Unable to parse response.");
        }      
        response.setText( handler.response );
    }
Offline  
Old 07-11-2007, 12:13 PM   #2 (permalink)
CrackBerry Addict
 
Join Date: Jun 2005
Location: Manchester, UK
Model: BOLD
Carrier: t-mobile
Posts: 714
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

here's some code from a template i created, have you actually implented a handler class?

try{
SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
InputStream in = new ByteArrayInputStream(serverResponse.getBytes());
InputSource inputSource = new InputSource(in);
saxParser.parse(in, new newBlackberrySoapSaxClassHandler());
}catch(Exception ex){

}

and then the handler:

static class newBlackberrySoapSaxClassHandler extends DefaultHandler {
String builder;
String temp;

public void startDocument() throws SAXException {
}

public void startElement(String uri, String name, String qName, Attributes atts){
if ("".equals(uri)){
System.out.println("Start element: " + qName);
}
temp = "";
}

public void endElement(String uri, String name, String qName){
if ("".equals(uri)){
System.out.println("End element: " + qName);
}
}

public void characters(char buf[],int offset,int len) throws SAXException {
temp="";
String temp2;
temp2 = new String(buf, offset, len).trim();
if (!temp2.equals("") || !temp2.equals(" ")){
temp = temp2;
}else{
temp = "";
}
}

public void endDocument() throws SAXException {

}
}
__________________
new job doesn't allow a public profile - please do not contact this user with questions, you will not get a response. good luck!
Offline  
Old 07-11-2007, 12:25 PM   #3 (permalink)
New Member
 
Join Date: Jul 2007
Model: 7100
PIN: N/A
Carrier: TMobile
Posts: 13
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

I did actually implemented the handler class. I've also saw your code from earlier post, but now it only outputs the element and nothing else. I've been fooling around with it but with no luck. I'm really new at this. Thanks for your help. Here's how my handler class look like

Code:
package com.sts.example;

import org.xml.sax.SAXException;
import org.xml.sax.helpers.DefaultHandler;
import org.xml.sax.Attributes;


/**
 * 
 */
public class ResponseHandler extends DefaultHandler {
  
    String builder;
    String temp;
    
    public void startDocument() throws SAXException {}
    
    public void startElement(String uri, String name, String qName, Attributes atts){
    if ("".equals(uri)){
    System.out.println("Start element: " + qName);
    }
    temp = "";
    }
    
    public void endElement(String uri, String name, String qName){
    if ("".equals(uri)){
    System.out.println("End element: " + qName);
    }
    }
    
    public void characters(char buf[],int offset,int len) throws SAXException {
        temp="";
        String temp2;
        temp2 = new String(buf, offset, len).trim();
        if (!temp2.equals("") || !temp2.equals(" ")){
            temp = temp2;
        }
        else{
            temp = "";
        }
    }
    
    public void endDocument() throws SAXException {}

}
Offline  
Old 07-11-2007, 12:43 PM   #4 (permalink)
New Member
 
Join Date: Jul 2007
Model: 7100
PIN: N/A
Carrier: TMobile
Posts: 13
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Jonathan,

I've found some good reading at here,
extends DefaultHandler : DefaultHandler : org.xml.sax.helpers : Java by API examples (example source code) Organized by topic
Hopefully I'll be this out. Thanks a lot for your input from earlier. Definitely pointed me at right direction. Much appreciated.
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.