XML mapping with java -
i have next xml:
<?xml version="1.0" encoding="utf-16"?> <root> <fieldset> <field id="c0" name="_id_version" datatype="int"/> <field id="c1" name="_type" datatype="string"/> <field id="c2" name="_product" datatype="string"/> <field id="c3" name="_version" datatype="string"/> <field id="c4" name="_revision" datatype="int"/> <field id="c5" name="_state" datatype="string"/> <field id="c6" name="_document" datatype="int"/> <field id="c7" name="_has_link" datatype="int"/> <field id="c8" name="_locked" datatype="int"/> <field id="c9" name="_accesslevel" datatype="int"/> <field id="c10" name="_label" datatype="int"/> <field id="c11" name="_label_name" datatype="string"/> </fieldset> <rowset> <row c0="2932" c1="" c2="{3fdac10d-5361-4c32-b79e-d391dcb3ae98}" c3="" c4="0" c5="" c6="0" c7="1" c8="0" c9="3" c10="-1" c11=""/> <row c0="3146" c1="0" c2="myproject" c3="" c4="0" c5="" c6="0" c7="1" c8="1" c9="3" c10="-1" c11=""/> <row c0="3147" c1="" c2="tes" c3="" c4="0" c5="" c6="0" c7="1" c8="0" c9="3" c10="-1" c11=""/> <row c0="2933" c1="" c2="" c3="" c4="0" c5="" c6="0" c7="1" c8="0" c9="3" c10="-1" c11=""/> <row c0="3048" c1="" c2="" c3="" c4="0" c5="" c6="0" c7="1" c8="0" c9="3" c10="-1" c11=""/> </rowset> </root>
how can map text pojo in java? have tried parsing next code:
documentbuilderfactory dbf = documentbuilderfactory.newinstance(); document document = null; seek { documentbuilder db = dbf.newdocumentbuilder(); inputsource = new inputsource(new stringreader(incomming)); document = db.parse(is); } grab (parserconfigurationexception | saxexception | ioexception e) { e.printstacktrace(); } homecoming document;
but want automatically.
this question hits of offtopic list. :) recommend-a-tool, opinion-based, what-have-you-tried-so-far-etc. but, hey, whatever. :)
here's opinion-based tool recommendation of like, namely jaxb.
write , annotate pojos:
@xmlrootelement(name="root") public class dataset { @xmlelementwrapper(name="fieldset") @xmlelement(name="field") public list<field> fields = new linkedlist<field>(); @xmlelementwrapper(name="rowset") @xmlelement(name="row") public list<row> rows = new linkedlist<row>(); } public class field { @xmlelement(name="id") public string id; @xmlelement(name="name") public string name; @xmlelement(name="datatype") public string datatype; } public class row { @xmlanyelement public map<qname, string> columns = new hashmap<qname, string>(); }
create jaxbcontext
pojos:
final jaxbcontext context = jaxbcontext.newinstance(dataset.class);
"parsing" in jaxb called "unmarshalling:
final unmarshaller = contect.createunmarshaller(); final dataset dataset = (dataset) unmarshaller.unmarshal(source);
i sketched code right here, won't work start, idea.
i not sure these pojos want, might start with.
apart jaxb, there's few dozens other libraries utilize task. please don't take jaxb answer, check alternatives well.
java xml mapping
No comments:
Post a Comment