java - Cannot read XML file using JAXB? -


having issues reading following xml file create.

<?xml version="1.0" encoding="utf-8" standalone="yes"?> <prsettings> <prsetting>     <players>         <player>             <lastdateentered>0</lastdateentered>             <lasttournament>1</lasttournament>             <playerid>0</playerid>             <playersstatus>unrated</playersstatus>             <playerstag>asfd</playerstag>             <score>5.0</score>             <setsplayed>0</setsplayed>             <tourneyswhileinactive>0</tourneyswhileinactive>         </player>         <player>             <lastdateentered>0</lastdateentered>             <lasttournament>1</lasttournament>             <playerid>1</playerid>             <playersstatus>unrated</playersstatus>             <playerstag>ba</playerstag>             <score>5.0</score>             <setsplayed>0</setsplayed>             <tourneyswhileinactive>0</tourneyswhileinactive>         </player>         <player>             <lastdateentered>0</lastdateentered>             <lasttournament>1</lasttournament>             <playerid>2</playerid>             <playersstatus>unrated</playersstatus>             <playerstag>asdgf</playerstag>             <score>5.0</score>             <setsplayed>0</setsplayed>             <tourneyswhileinactive>0</tourneyswhileinactive>         </player>     </players>     <challongeapikey>asbg</challongeapikey>     <challongeusername>asf</challongeusername>     <implementpointdecay>false</implementpointdecay>     <numsetsneeded>5</numsetsneeded>     <numtourneysforactive>2</numtourneysforactive>     <numtourneysforinnactive>5</numtourneysforinnactive>     <numtourneysneeded>5</numtourneysneeded>     <pointsremoved>5</pointsremoved>     <prname>asf</prname>     <removeinnactive>false</removeinnactive>     <showplacingdiff>false</showplacingdiff>     <showpointdiff>false</showpointdiff>     <startofdecay>3</startofdecay> </prsetting> 

i have observablelist of prsetting objects , within prsetting objects have arraylist of players. why created pojo file , within prsetting object object set following.

@xmlelementwrapper(name="players") @xmlelement(name ="player") private arraylist<playerprofile> playerslist = new arraylist<playerprofile>(); 

here pojo file supposed used write , read xml file.

@xmlrootelement (name = "prsettings") public class prsettingswrapper { private observablelist<prsettings> prlist;  @xmlelement(name = "prsetting") public observablelist<prsettings> getprlist(){     return prlist; }  public void setprlist(observablelist<prsettings> prlist){     this.prlist = prlist; } } 

for reason whenever attempt to load data following code

       jaxbcontext context = jaxbcontext                .newinstance(prsettingswrapper.class);        unmarshaller um = context.createunmarshaller();         // reading xml file , unmarshalling.        prsettingsdata wrapper = (prsettingsdata) um.unmarshal(file);         prlist.clear();        prlist.addall(wrapper.getprlist());         // save file path registry.        setprsettingsfilepath(file); 

i cannot seem load xml files objects. file path working correctly, i'm not sure i'm doing wrong.

thank in advance help.

sorry mislead cause.

the problem caused observablelist;

you can refer article: marshalling observablelist jaxb

i modified code link above, , followings should workable.

anothertest.mycontainer

package anothertest;  import java.util.list;      import javafx.collections.fxcollections;     import javafx.collections.observablelist;      import javax.xml.bind.annotation.xmlelement;     import javax.xml.bind.annotation.xmlelementwrapper;     import javax.xml.bind.annotation.xmlelements;     import javax.xml.bind.annotation.xmlrootelement;      @xmlrootelement(name = "prsettings")     public class mycontainer extends myobject {          private observablelist<myobject> children = fxcollections.observablearraylist();          @xmlelements({ @xmlelement(name = "prsetting", type = myobject.class) })         public list<myobject> getchildren() {             return children;         }          public string tostring() {              stringbuilder sb = new stringbuilder();             sb.append("children:");              (myobject node : children) {                 sb.append("\n");                 sb.append("  " + node.tostring());             }             return sb.tostring();          }     } 

anothertest.myobject

    package anothertest;      import java.util.list;      import javax.xml.bind.annotation.xmlelement;     import javax.xml.bind.annotation.xmlelementwrapper;     import javax.xml.bind.annotation.xmltype;      @xmltype(name = "object")     public class myobject {          private string challongeapikey;          @xmlelementwrapper(name = "players")         private list<playerprofile> player;          public string getchallongeapikey() {             return challongeapikey;         }          public void setchallongeapikey(string challongeapikey) {             this.challongeapikey = challongeapikey;         }          public list<playerprofile> getplayer() {             return player;         }          public void setplayers(list<playerprofile> player) {             this.player = player;         }      } 

anothertest.playerprofile

package anothertest;  public class playerprofile {      private int playerid;     private string playersstatus;      public int getplayerid() {         return playerid;     }      public void setplayerid(int playerid) {         this.playerid = playerid;     }      public string getplayersstatus() {         return playersstatus;     }      public void setplayersstatus(string playersstatus) {         this.playersstatus = playersstatus;     }  } 

anothertest.example

package anothertest;  import java.io.stringreader; import java.io.stringwriter; import java.util.arraylist; import java.util.list;  import javax.xml.bind.jaxbcontext; import javax.xml.bind.marshaller; import javax.xml.bind.unmarshaller;  public class example {     public static void main(string[] args) {          // create container list         mycontainer container = new mycontainer();          // add objects         myobject object;          object = new myobject();         object.setchallongeapikey("abcabc");         container.getchildren().add(object);          playerprofile p = new playerprofile();         p.setplayerid(1);         p.setplayersstatus("unrated");          list<playerprofile> l = new arraylist<>();         l.add(0, p);          object.setplayers(l);          // marshal         string basexml = marshal(container);          // unmarshal         container = unmarshal(basexml);          system.out.println("unmarshal test: " + container.getchildren().get(0).getchallongeapikey());         system.out.println("unmarshal test: " + container.getchildren().get(0).getplayer().get(0).getplayerid());         system.out.println("unmarshal test: " + container.getchildren().get(0).getplayer().get(0).getplayersstatus());          system.exit(0);     }      public static string marshal(mycontainer base) {          try {              jaxbcontext jaxbcontext = jaxbcontext.newinstance(mycontainer.class);              marshaller jaxbmarshaller = jaxbcontext.createmarshaller();             jaxbmarshaller.setproperty(marshaller.jaxb_formatted_output, true);              stringwriter stringwriter = new stringwriter();             jaxbmarshaller.marshal(base, stringwriter);             string xml = stringwriter.tostring();              system.out.println("xml:\n" + xml);              return xml;          } catch (exception e) {             throw new runtimeexception(e);         }      }      public static mycontainer unmarshal(string xml) {          try {              jaxbcontext jaxbcontext = jaxbcontext.newinstance(mycontainer.class);              unmarshaller jaxbunmarshaller = jaxbcontext.createunmarshaller();             stringreader stringreader = new stringreader(xml);              mycontainer container = (mycontainer) jaxbunmarshaller.unmarshal(stringreader);              return container;          } catch (exception e) {             throw new runtimeexception(e);         }      } } 

the output in console is

xml: <?xml version="1.0" encoding="utf-8" standalone="yes"?> <prsettings>     <prsetting>         <players>             <player>                 <playerid>1</playerid>                 <playersstatus>unrated</playersstatus>             </player>         </players>         <challongeapikey>abcabc</challongeapikey>     </prsetting> </prsettings>  unmarshal test: abcabc unmarshal test: 1 unmarshal test: unrated 

sorry, don't know why either. tested if declare children list<myobject>, can work properly. however, when comes observablelist, must declare this, or nullpointerexception occur when unmarshalling (but same code works on marshalling).

private observablelist<myobject> children = fxcollections.observablearraylist(); 

Comments

Popular posts from this blog

java - Run spring boot application error: Cannot instantiate interface org.springframework.context.ApplicationListener -

reactjs - React router and this.props.children - how to pass state to this.props.children -

Excel VBA "Microsoft Windows Common Controls 6.0 (SP6)" Location Changes -