You don't need xml to represent hierarchies.
You can represent your notes like this, and write your data in this
way (if you want to use binary):
public static byte[] encode(final ArrayList<Note> notes) throws
IOException {
int size = notes.size();
ByteArrayOutputStream out = new ByteArrayOutputStream
(NOTE_AVERAGE_SIZE * size);
DataOutputStream data = new DataOutputStream(out);
for (int i = 0; i < size; i++) {
Note note = notes.get(i);
data.writeUTF(note.parentFolder != null ?
note.parentFolder : "");
data.writeUTF(note.title != null ? note.title : "");
data.writeUTF(note.text != null ? note.text : "");
}
data.flush();
return out.toByteArray();
}
public static final class Note {
public Note(String parentFolder, String title, String text) {
this.parentFolder = parentFolder;
this.title = title;
this.text = text;
}
public String parentFolder;
public String title;
public String text;
}
All you need to do when you read is to use a Map where you place notes
that belong to a certain folder. You can have "unlimited" depth if you
do that.
On 22 Juli, 22:34, gnugu <[email protected]> wrote:
> Richard,
> thanks for suggestion.
>
> My notes are structured. The app allows for the notes to be in the
> folders one level deep. That's why I opted for XML.
>
> I'll try to find out the presentation and post the link here for
> others.
>
> On Jul 22, 1:17 pm, Richard Schilling <[email protected]>
> wrote:
>
>
>
> > Yes, there were some good suggestions. And, yes, using the database
> > is a great idea. But I have another, and I use this general approach
> > a lot when I need to import/export data or just ship files around on a
> > device, or when memory and performance are key.
>
> > XML parsers, streaming or in-memory DOM are time consuming and often
> > not ncessary when you're working with simple data like strings. XML
> > is best used when you need to tag the data with a lot of attributes.
> > Not only that, but XML bloats the memory requirements of your
> > application multiple times because you have to save all XML characters
> > (e.g. '<', '=').
>
> > You'll get the most speed out of processing, especially I/O operations
> > if you first reduce each note to a binary form (e.g. array of
> > characters). If you need to store all the notes in a single file,
> > then I would suggest some format like this:
>
> > HEADER LENGTH STRING HEADER LENGTH STRING .... .etc .....
>
> > Here's some code.
>
> > public static byte[] encode(String s){
>
> > byte[] header = "CAFEBABE".getBytes(); // or some other
> > magic token
> > byte[] chars = s.getBytes();
> > byte[] sizeBytes = intToByteArray(chars.length); // convert
> > an int
> > to a byte array - defined elsewhere.
>
> > int totalLength = header.length + chars.length +
> > sizeBytes.length;
> > ByteBuffer buf = ByteBuffer.allocate(totalLength);
> > buf.put(header);
> > buf.put(sizeBytes);
> > buf.put(chars);
>
> > buf.position(0);
> > return buf.array();
> > }
>
> > Pass each string in, and then pack the results from each call into
> > your file.
>
> > Richard Schilling
> > Root Wireless
>
> > On Jul 22, 12:57 pm, gnugu <[email protected]> wrote:
>
> > > > Just creating 1000 objects isn't going to take 45 seconds, it sounds
> > > > like
>
> > > I was running it in the emulator with the debugger attached. I'll
> > > check logs and try the profiler.
>
> > > Thanks.
>
> > > On Jul 22, 12:02 pm, Dianne Hackborn <[email protected]> wrote:
>
> > > > Just creating 1000 objects isn't going to take 45 seconds, it sounds
> > > > like
> > > > you have something really bad going on somewhere. With it taking that
> > > > amount of time, you will probably quickly see some serious hot spots if
> > > > you
> > > > run the profiler.
>
> > > > Also if you are seeing log messages about the GC running during that
> > > > time,
> > > > this means that something in your code is creating tons and tons of
> > > > temporary objects. Again the profiler should help a lot in tracking
> > > > this
> > > > down.
>
> > > > On Wed, Jul 22, 2009 at 11:39 AM, gnugu <[email protected]> wrote:
>
> > > > > Dianne,
>
> > > > > It is not reading and writing that's slow.
>
> > > > > I have a class that has Notes field (ArrayList<Note>). I wrote my test
> > > > > to create 1000 note objects and add it took 45 seconds.
> > > > > Because I can't get XML string out of DOM Document I can't use the
> > > > > Document as my storage for notes which leaves me with creating all
> > > > > these objects as I read the XML.
>
> > > > > It would be ideal for me to use MySQL to store the notes but then I
> > > > > would want to encrypt the table as a whole rather then individual
> > > > > records.
>
> > > > > My own binary format might work if I create my own data reader that
> > > > > would work on my binary data. That's a lot of work.
>
> > > > > Hmm, I wonder if I could simply encrypt the database file. But then
> > > > > how do I feed it to DatabaseHelper?
>
> > > > > On Jul 22, 11:26 am, Dianne Hackborn <[email protected]> wrote:
> > > > > > Which part is slow, reading or writing? If it is writing, just
> > > > > > stay the
> > > > > > heck away from the standard XML writers, they are horrendous.
> > > > > > There is a
> > > > > > FastXmlSerializer with limited functionality that you could copy
> > > > > > out of
> > > > > the
> > > > > > source code for your own use:
>
> > > > > >http://android.git.kernel.org/?p=platform/frameworks/base.git;a=blob;...
>
> > > > > > That said, XML is just pretty intrinsically inefficient to read and
> > > > > write.
> > > > > > I have been yelled at more than once about using it at all. :) If
> > > > > > the
> > > > > > amount of data you are storing is relatively large, you may want to
> > > > > > just
> > > > > use
> > > > > > your own binary format. Yes, this does require a lot more work to
> > > > > > deal
> > > > > with
> > > > > > compatibility as the format changes and such, but often that is
> > > > > > worth it,
>
> > > > > > On Wed, Jul 22, 2009 at 9:23 AM, gnugu <[email protected]> wrote:
>
> > > > > > > Hi,
> > > > > > > I'm working on an application where I want to save bunch of
> > > > > > > information in the XML file. I am not using MySQL because I want
> > > > > > > all
> > > > > > > the info to be encrypted and in one place. I don't know how this
> > > > > > > could
> > > > > > > be done in the data base.
>
> > > > > > > I was thinking that I would use DOM Document for my data adapter
> > > > > > > to
> > > > > > > bind with ListView. But I found out that there is no way of
> > > > > > > getting
> > > > > > > the XML out of DOM Document to save it to the file!
>
> > > > > > > So now I use SAX to read XML and create bunch of objects one for
> > > > > > > each
> > > > > > > piece of data in memory.
>
> > > > > > > The SDK Guide in Designing for Performance says to avoid creating
> > > > > > > objects (http://developer.android.com/guide/practices/design/
> > > > > > > performance.html#object_creation<
> > > > >http://developer.android.com/guide/practices/design/%0Aperformance.ht...>
> > > > > > > ).
>
> > > > > > > I understand and agree and even proved that the performance sucks
> > > > > > > if I
> > > > > > > do it the way I described above.
>
> > > > > > > What other choice there might be? Anybody has any suggestions how
> > > > > > > I
> > > > > > > could save bunch of notes and have them all encrypted with ONE
> > > > > > > password as a key base? I know I could use that one key to encrypt
> > > > > > > every database row, but then changing the password would be
> > > > > > > difficult.
>
> > > > > > > Any suggestions?
>
> > > > > > > Thanks.
>
> > > > > > --
> > > > > > Dianne Hackborn
> > > > > > Android framework engineer
> > > > > > [email protected]
>
> > > > > > Note: please don't send private questions to me, as I don't have
> > > > > > time to
> > > > > > provide private support, and so won't reply to such e-mails. All
> > > > > > such
> > > > > > questions should be posted on public forums, where I and others can
> > > > > > see
> > > > > and
> > > > > > answer them.
>
> > > > --
> > > > Dianne Hackborn
> > > > Android framework engineer
> > > > [email protected]
>
> > > > Note: please don't send private questions to me, as I don't have time to
> > > > provide private support, and so won't reply to such e-mails. All such
> > > > questions should be posted on public forums, where I and others can see
> > > > and
> > > > answer them.
--~--~---------~--~----~------------~-------~--~----~
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to [email protected]
To unsubscribe from this group, send email to
[email protected]
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en
-~----------~----~----~----~------~----~------~--~---