Thursday, 15 May 2014

java - What API to choose for data serialization to xml? -



java - What API to choose for data serialization to xml? -

this question has reply here:

what simplest , minimalistic java xml api? 7 answers

i need save user info between sessions. decided using binary serialization , start saving info in xml. after research found several apis, namely jaxb , xstream.

i looked through samples of xstream , it. simple. in 2 words: give object , receive .xml representation of object. read xml , receive object back.

then read jaxb - strong, functional. examples found creating of xml schema, generating java classes basing on schema , on. @ moment looks little bit time consuming me describe classes in .xsd. hope 1 of many sides of jaxb usage. saw, feared me little.

are there other apis suit task. or pros , cons of jaxb , xstream then?

i start jaxb built-in , easy use. not need start xsd. add together annotations classes.

@xmlrootelement(name="doc") public class document { @xmlelement protected foo foo; // ... }

serialization:

document doc = new document(); jaxbcontext jc = jaxbcontext.newinstance(document.class); marshaller m = jc.createmarshaller(); m.marshal(doc, system.out);

deserialization:

jaxbcontext jc = jaxbcontext.newinstance(document.class); unmarshaller u = jc.createunmarshaller(); document doc = u.unmarshal(system.in);

replace system.out , system.in actual streams , ready go.

there short tutorial regarding jaxb annotations in jaxb tutorial:

http://jaxb.java.net/tutorial/section_6_2_1-a-survey-of-jaxb-annotations.html

java

No comments:

Post a Comment