Skip to main content

how generate xml from java object in android



i have a array list with values. The user will add some values to my ArrayList and i want to serialize it to xml file to response to server to avoid using a database for this little option and of course TO LEARN how to serialize (i'm a begginer) because it seems useful. Anyways the user save something in the arraylist, the program shuts down, the program starts up again and the user is able to see the saved data. How can i implement this? Can you provide some code snippet or a useful link?





Thanks a lot!!


Comments

  1. The serialization will happen automatically as long as the class is implementing the serializable interface. You only have to step into the process when say you are using another class that for some reason you cannot change to implement serializable. In that case you actually step into the serialization process and serialize it manually. Also ivars with the transient qualifier will not be serialized.

    That being said you will pretty much have to generate the xml file yourself, first agree on an schema and then simply generate a a xml String based on what you need.

    Alternatively you can just use a JSONARRAY and make your life easier.

    ReplyDelete
  2. import java.beans.XMLEncoder;
    import java.beans.XMLDecoder;
    import java.io.*;

    public class XMLSerializer {
    public static void write(Object f, String filename) throws Exception{
    XMLEncoder encoder =
    new XMLEncoder(
    new BufferedOutputStream(
    new FileOutputStream(filename)));
    encoder.writeObject(f);
    encoder.close();
    }

    public static Object read(String filename) throws Exception {
    XMLDecoder decoder =
    new XMLDecoder(new BufferedInputStream(
    new FileInputStream(filename)));
    Object o = (Object)decoder.readObject();
    decoder.close();
    return o;
    }
    }


    When serialize objects need to remember this: The object must implement the Serializable and have a default constructor (otherwise it can not be deserialize).

    ReplyDelete

Post a Comment

Popular posts from this blog

Why is this Javascript much *slower* than its jQuery equivalent?

I have a HTML list of about 500 items and a "filter" box above it. I started by using jQuery to filter the list when I typed a letter (timing code added later): $('#filter').keyup( function() { var jqStart = (new Date).getTime(); var search = $(this).val().toLowerCase(); var $list = $('ul.ablist > li'); $list.each( function() { if ( $(this).text().toLowerCase().indexOf(search) === -1 ) $(this).hide(); else $(this).show(); } ); console.log('Time: ' + ((new Date).getTime() - jqStart)); } ); However, there was a couple of seconds delay after typing each letter (particularly the first letter). So I thought it may be slightly quicker if I used plain Javascript (I read recently that jQuery's each function is particularly slow). Here's my JS equivalent: document.getElementById('filter').addEventListener( 'keyup', function () { var jsStart = (new Date).getTime()...

Is it possible to have IF statement in an Echo statement in PHP

Thanks in advance. I did look at the other questions/answers that were similar and didn't find exactly what I was looking for. I'm trying to do this, am I on the right path? echo " <div id='tabs-".$match."'> <textarea id='".$match."' name='".$match."'>". if ($COLUMN_NAME === $match) { echo $FIELD_WITH_COLUMN_NAME; } else { } ."</textarea> <script type='text/javascript'> CKEDITOR.replace( '".$match."' ); </script> </div>"; I am getting the following error message in the browser: Parse error: syntax error, unexpected T_IF Please let me know if this is the right way to go about nesting an IF statement inside an echo. Thank you.