Tuesday, 15 September 2015

java - Trying to convert an object array into XML, can't get object headers -



java - Trying to convert an object array into XML, can't get object headers -

i'm using xml converter, not getting object header wrap each object's properties... can't find method in encoder class either.

the code iterates through array , lists objects not null.

class="lang-java prettyprint-override">fileoutputstream os = new fileoutputstream("c:\\users\\david laptop\\documents\\doc1.xml"); xmlencoder encoder = new xmlencoder(os); for( int x = 0; x < people.length;x++) if (people[x] != null) { //header here? encoder.writeobject(people[x].getname()); encoder.writeobject(people[x].gettelephonenumber()); encoder.writeobject(people[x].getemailaddress()); } } encoder.close();

i outcome:

class="lang-xml prettyprint-override"><?xml version="1.0" encoding="utf-8" ?> <java version="1.7.0_40" class="java.beans.xmldecoder"> string dad</string string 35235 /string string email /string </java>

if more object entries ends beingness big list isn't helpful function want implement reading xml file array... help on useful!

edit: new info based on reply given:

so there no way create happen without no-arg constructor? i've implemented serializable both classes measure... i'm using line add together new objects:

class="lang-java prettyprint-override">mybook1.addrecord(new newperson(name,telephonenumber,emailaddress));

which uses this:

class="lang-java prettyprint-override">public void addrecord(newperson c) { people[numofrecords] = c; numofrecords++; }

below object itself:

class="lang-java prettyprint-override">public class newperson implements java.io.serializable { private string name; private string telephonenumber; private string emailaddress; public newperson(string n, string t, string e) { //local variables n,t,e used in method name = n; telephonenumber = t; emailaddress = e; }

any suggestions?

serializing object instance variables result in hard main process forced decode values 1 one.

it wiser , easier deal when serializing whole people objects:

fileoutputstream os = new fileoutputstream("c:\\users\\david laptop\\documents\\doc1.xml"); xmlencoder encoder = new xmlencoder(os); for( int x = 0; x < people.length;x++) if (people[x] != null) { encoder.writeobject(people[x]); } } encoder.close();

meanwhile, have create sure people class statisfies javabeans conventions basiclly has serializable , should provide public no-arg constructor.

java arrays xml object xmlencoder

No comments:

Post a Comment