2012-12-13

JAXB example

You should get a point of how to use JAXB after reading through this code.

XML content to read

01.<jawabotConfig>
02. 
03.   <settings
04.      verbose = "true"
05.      unsecuredShutdown = "false"
06.      acceptInvitation = "true"
07.      messageDelay = "1000"
08.      leaveOnAsk = "true"
09.      adminUser = "ozizka"
10.      smtpHost = "smtp.somewhere.com"
11.      announceEmailTo = "announce@somewhere.com"
12.      announceEmailFrom = "jawabot-no-reply@somewhere.com"
13.      announceDefaultChannel = "#jboss"
14.      debugChannel = "#some"
15.   />
16. 
17.   <irc>
18.      <defaultNick>JawaBot-beta</defaultNick>
19.      <defaultNick2>JawaBot-beta_</defaultNick2>
20.      <server host="irc.freenode.net">
21.         <autoJoinChannels>
22.            #richfaces
23.            #jboss
24.         </autoJoinChannels>
25.      </server>
26.   </irc>
27. 
28.   <resources>
29.      <resource name="jawa01" note="EAP"/>
30.      <resource name="jawa02" note="EAP"/>
31.   </resources>
32. 
33.   <resourceGroups>
34.      <group name="brno" channel="#brno" mail="jboss@somewhere.com"> jawa01 jawa02 ...</group>
35.      <group name="openjdk">jawa16 jawa28 jawa29</group>
36.   </resourceGroups>
37. 
38.   <userGroups>
39.      <group name="eap">ozizka rhusar pslavice istudens</group>
40.      <group name="openjdk">ptisnovs</group>
41.   </userGroups>
42. 
43.</jawabotConfig>

Root JAXB bean

01.@XmlRootElement(name="jawabotConfig")
02.public class ConfigBean {
03. 
04.   @XmlElement
05.   public SettingsBean settings;
06. 
07.   @XmlElement
08.   public IrcBean irc;
09. 
10.   @XmlElementWrapper(name="resources")
11.   @XmlElement(name="resource")
12.   public List<Resource> resources;
13.   public List<Resource> getResources() {      return resources;   }
14. 
15. 
16.   @XmlElement
17.   public ResourceGroupsBean resourceGroups;
18. 
19.   @XmlElement
20.   public UserGroupsBean userGroups;
21. 
22.}// class

Sub-beans for <settings>, <irc>, <resourceGroups>

01.public class SettingsBean {
02. 
03.   @XmlAttribute public boolean verbose = true;
04.   @XmlAttribute public boolean acceptInvitation = true;
05.   @XmlAttribute public int messageDelay = 666;
06.   @XmlAttribute public boolean leaveOnAsk = true;
07.   @XmlAttribute public String adminUser = "ozizka";
08.   @XmlAttribute public String smtpHost = "...";
09.   @XmlAttribute public String announceEmailTo = "...";
10.   @XmlAttribute public String announceEmailFrom = "...";
11.   @XmlAttribute public String announceDefaultChannel = "#jboss";
12.   @XmlAttribute public String debugChannel = "#some";
13.   @XmlAttribute public boolean unsecuredShutdown = false;
14. 
15.}// class
1.public class IrcBean implements Serializable {
2. 
3.   @XmlElement public String defaultNick;
4. 
5.   @XmlElement(name="server") public List<ServerBean> servers;
6. 
7.}// class
01.public class ServerBean {
02. 
03.   @XmlAttribute public String host;
04.   @XmlAttribute public Integer port;
05. 
06.   @XmlElement
07.   @XmlList public List<String> autoJoinChannels;
08. 
09.}// class
01.public class ResourceGroupsBean implements Serializable {
02. 
03.   /*
04.   <resourceGroups>
05.      <group name="soa">jawa01 jawa02 jawa03</group>
06.      <group name="embjopr">jawa18</group>
07.   </resourceGroups>
08.   */
09. 
10.   @XmlElement(name="group")
11.   List<ResourceGroupBean> groups;
12. 
13.}// class

Code which reads the XML into the bean

1.String filePath = "myFile.xml"
2.InputStream is = JawaBot.class.getClassLoader().getResourceAsStream(filePath);
3.InputStreamReader reader = new InputStreamReader( is );
4. 
5.JAXBContext jc = JAXBContext.newInstance( ConfigBean.class );
6.Unmarshaller mc = jc.createUnmarshaller();
7.ConfigBean config = (ConfigBean) mc.unmarshal(reader);

0