How to save all variables, properties & attributes into XML or ???
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 ?
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.
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.
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)
There are people who think otherwise, but that’s my personal take.
I appreciate opinions :)
Many thanks again.
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 :)
does it require a lot of code and specific one for each objects types etc ?
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.
any code available to see a bit how it works?
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();
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
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)
catch (IOException e)
Your exact approach will entirely depend on the structure of the XML itself.
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..
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.
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.
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..)
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.
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..
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!
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.
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..
Forums > Java