BlackBerry Forums Support Community               

Closed Thread
 
LinkBack Thread Tools
Old 02-19-2009, 04:48 AM   #1 (permalink)
Thumbs Must Hurt
 
Join Date: May 2007
Location: berlin, germany
Model: 8310
PIN: N/A
Carrier: vodafone
Posts: 163
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default XML serializer: XML -> myClass

Please Login to Remove!

Hi,
in vb.net there is a way to serialize a class to a XML-document and the other way round.
Is there also a simple way to do this in java for BlackBerry? I get a XML-document such like this one here:
Quote:
<myDoc>
<user>
<name>Name1</name>
<vorname>Vorname1</vorname>
</user>
<user>
<name>Name2</name>
<vorname>Vorname2</vorname>
</user>
...
</myDoc>
Now I've got my Class user:
Code:
class user {
public String Name;
public String Vorname;
user() {}
}
And now i want to pasre the XML-String and after parsing I want to have a Vector. The Vector should contain n elements of class user, created from the XML-String.

So is there a Simple way to do this? Is there a kind of function which I can use for other classes also??

I hope you know what i want to do, and i hope that someone can help me

thanks, hibbert
__________________
My English is so bad, that makes me nobody so quickly after
Offline  
Old 02-19-2009, 05:11 AM   #2 (permalink)
Thumbs Must Hurt
 
Join Date: Feb 2009
Model: 9000
PIN: N/A
Carrier: T-Mobile
Posts: 67
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

use the native sax parser: net.rim.device.api.xml.parsers.SAXParser

SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();
Offline  
Old 02-19-2009, 05:21 AM   #3 (permalink)
Thumbs Must Hurt
 
Join Date: May 2007
Location: berlin, germany
Model: 8310
PIN: N/A
Carrier: vodafone
Posts: 163
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

Ok thanks, i will take a look at it

But the only Examples i could find doesn't return a class or something like that or I don't understand what is done there, so it really doesn't help me at the moment. maybe you can give me another tip how to complete this

thanks again hibbert
__________________
My English is so bad, that makes me nobody so quickly after
Offline  
Old 02-19-2009, 06:10 AM   #4 (permalink)
Thumbs Must Hurt
 
Join Date: May 2007
Location: berlin, germany
Model: 8310
PIN: N/A
Carrier: vodafone
Posts: 163
Post Thanks: 0
Thanked 0 Times in 0 Posts
Default

ok, this is what i have got (so far)
PHP Code:
public class frmXML extends UiApplication{
    
MainScreen mainScreen null;
        
    private final class 
myScreen extends MainScreen {}
     
    public static 
void main(Stringxxx91;xxx93; args) {
        
frmXML theApp = new frmXML();
        
theApp.enterEventDispatcher();
    }
    
    private 
InputStream stringToStream(String _text) {
        
InputStream is = new ByteArrayInputStream(_text.getBytes("UTF-8"));
        return 
is;
    }
        

    public 
frmXML() {
        final 
MainScreen mainScreen = new myScreen();
        
SAXParserFactory factory SAXParserFactory.newInstance();
        
OutputStreamWriter out;      
        
String xmlString "<data><user><name>Name1</name><vorname>Vorname1</vorname></user><user><name>Name2</name><vorname>Vorname2</vorname></user></data>";
  try {

        
out = new OutputStreamWriter(System.out"UTF8");
        
SAXParser saxParser factory.newSAXParser();
        
saxParser.parse(new InputSource(stringToStream(xmlString), new clsUser()));
    } catch (
Throwable err) {
        
err.printStackTrace ();
  }
}
}
//========================================
package XML;

class 
clsUser 
    public 
String Name "";
    public 
String Vorname "";
    
clsUser() {}

but i get this error here:
Quote:
cannot find symbol
symbol : constructor InputSource(java.io.InputStream,XML.clsUser)
location: class org.xml.sax.InputSource
saxParser.parse(new InputSource(stringToStream(xmlString), new clsUser()));
^ (under new InputSource)
1 error
maybe someone can help me, and how do i create my vector??

thanks hibbert
__________________
My English is so bad, that makes me nobody so quickly after
Offline  




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