BlackBerry Forums Support Community

BlackBerry Forums Support Community (http://www.blackberryforums.com/)
-   Developer Forum (http://www.blackberryforums.com/developer-forum/)
-   -   XML serializer: XML -> myClass (http://www.blackberryforums.com/developer-forum/177078-xml-serializer-xml-myclass.html)

hibbert 02-19-2009 03:48 AM

XML serializer: XML -> myClass
 
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

hippocamp software 02-19-2009 04:11 AM

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

SAXParserFactory factory = SAXParserFactory.newInstance();
SAXParser saxParser = factory.newSAXParser();

hibbert 02-19-2009 04:21 AM

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

hibbert 02-19-2009 05:10 AM

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


All times are GMT -5. The time now is 09:29 PM.

Powered by vBulletin® Version 3.6.12
Copyright ©2000 - 2014, Jelsoft Enterprises Ltd.