Forums > Java

How to save all variables, properties & attributes into XML or ???

May 24, 2012 | 4:51 pm

Hi there,
designing a system using Java intensively to create/remove/edit 3D (jit.gl.gridshape) objects, I’d need to save all positions and all attributes of those objects into preset (basically, I’m calling a preset a "map" in my system)

Is there a native way related to the Java implementation in Max or should I design dump/restore methods ?


May 25, 2012 | 11:22 am

You could use object serialisation. This is extremely quick and allows you to save and load very complicated data with only a few lines of code.

http://www.javabeginner.com/uncategorized/java-serialization

It can be even easier if you use a lot of the java.util objects, which frequently eases programming in a number of ways. E.g. built in variable length arrays, hashtables, etc.

I’m a big fan of readable XML. So if it were me I would dump XML files, but then have to write code (using the built in XML parsers) to recover the data from the XML file.

I have quite frequently built applications using object serialisation early on, then written the XML handling code later on.

If anyone knows of any generic code for building and unpacking Java objects to XML, perhaps based on introspection, I’d like to hear about them.


May 25, 2012 | 11:30 am

Many thanks Ross for your infos.
Serialization seems to be THE way.
I’d probably begin with it, then go to XML.

This could be a good point for me to go with JAVA instead of JS too (still not sure about that. JS seems fast enough with my early prototype)


May 25, 2012 | 11:39 am

I personally think that Java is a much better language than JavaScript for making really large applications as I think that object orientation is better integrated in Java.

There are people who think otherwise, but that’s my personal take.


May 25, 2012 | 11:40 am

I appreciate opinions :)

Many thanks again.


May 25, 2012 | 11:53 am

This looks useful, particularly in "introspection mode". I’m going to give it a try.

http://www.castor.org/xml-framework.html


May 25, 2012 | 1:47 pm

I’ll test it asap.
I have to figure out how to use it.
I love the idea of dump everything rawly like that and to retrieve it too.

In my case, I’ll have a lot of objects, not only instantiated from the same class, so I’d like to experiment to dump all in a same XML file etc.

let me know if you test it :)


May 25, 2012 | 10:31 pm

I’ve used JDOM extensively with Java in Max and it works quite well to write/read XML.

http://www.jdom.org/


May 25, 2012 | 10:37 pm

Hi Jesse,
does it require a lot of code and specific one for each objects types etc ?


May 25, 2012 | 11:07 pm

I was mostly parsing pre-existing XML, which took a bit of trial and error since they were not created according to any published standard. The final code is fairly compact and readable.

I was using the older version of JDOM, so I’m not as familiar with the changes in v. 2.0. But with the older version once you understand the API it’s pretty lightweight.


May 25, 2012 | 11:13 pm

any code available to see a bit how it works?


May 25, 2012 | 11:35 pm

I can’t post the full code because it was built for a client, but the essential elements are here. I was accessing an XML file over http, you can do this with local files as well.

SAXBuilder parser = new SAXBuilder();

try
{
Document doc = parser.build( URL );

// top level XML container
Element root = doc.getRootElement();

// "data" subcontainer, assuming that's what you're looking for
Element data = root.getChild( "data" );

// child nodes
Iterator iter = data.getChildren().iterator();
while( iter.hasNext() )
{
Element e = (Element)iter.next();
String name = e.getName();
String value = e.getValue(); // you can parse this to another data type as needed

// ......
}
}
catch (JDOMException e)
{
e.printStackTrace();
}
catch (IOException e)
{
e.printStackTrace();
}

Your exact approach will entirely depend on the structure of the XML itself.


May 25, 2012 | 11:41 pm

with castor, afaik, it is possible to directly "dump" whole objects inside.
check the introspection mode : http://www.castor.org/xml-framework.html#Introspection-mode

sounds a bit nebulous for me but I got it, I guess..

the idea of push ALL objects inside one bigger, then to dump seems safe for me.
but maybe it is a bit utopic to imagine I would not have to take each attributes separately to build my xml etc etc..


May 26, 2012 | 5:08 pm

I’d be surprised if you could push a top-level object to XML and then recreate it on read, with all child objects correctly instantiated. It’s also unclear from the example code how you would retrieve multiple instances of the same type of object, which I gather you will want to do in your project.

If you do get this to work please let us know, it would be useful.


May 26, 2012 | 9:27 pm

Hello Jesse,
I know myself too much: I’ll probably design my own simple but efficient system .

The thing is: I’d like to know how to keep it evolvable.

When I’ll create new classes, I’d like to have only a basic copy paste + light tweak in order to be ok.
No More.

I wouldn’t go to a system which would involve classes dependencies checking etc … after all, it will be my own system and even if I have in head around 6 or 7 different objects, I wouldn’t add 150 more.. so I’ll be able to
1/ maitain the dump/restore system easily
2/ keep the whole classes system in each project (even those who won’t use more than one class amongst all the other..)


May 27, 2012 | 9:25 am

This may be late in the discussion, but: what do you gain by using Java as source language rather than a simpler, more flexible, less verbose JVM-hosted language like Python or Clojure? You get access to the Java API and less classpath hassle.

Python can serialise (via the pickle library); I’ve not done persistence in Clojure but there are lots of tools out there.


May 27, 2012 | 9:48 am

Hey Nick, no: you are never too late!

I won’t go to python for many reasons BUT I would consider clojure maybe.
Clojure is based on java afaik and I guess I can instantiate anything from jitter too etc..

About serialization.
Is it utopic to imagine a real "automatic" system that would dump/restore every classes/subclasses etc ?

As suggested by Jesse 2 messages before, I’m not sure it would be possible to reinstiante everything etc.
I’d just … like it!


May 27, 2012 | 10:53 am

What’s wrong with Python?

Clojure isn’t "based on" Java – it’s a dialect of Lisp – but it runs on the JVM and provides access to all the Java libraries.

When it comes to things which are "automatic": be careful what you wish for.


May 27, 2012 | 12:06 pm

Nothing wrong with python except me.
I used it only for ableton remote scripts and a coupe of network stuff (with twisted framework) and it would create a gap too big between me and my project which would be too hard for me :-/

I’m writing that probably because I’m not able to understand the benefits underneath..


Viewing 19 posts - 1 through 19 (of 19 total)