CHPP Development/Java/JAXB: Difference between revisions

From Hattrick
Jump to navigationJump to search
Filipx (talk | contribs)
No edit summary
 
Filipx (talk | contribs)
No edit summary
Line 9: Line 9:
3. Create Java classes with the following command:
3. Create Java classes with the following command:
  C:\Sun\jwsdp-1.6\jaxb\bin\xjc.bat players.xsd -p players
  C:\Sun\jwsdp-1.6\jaxb\bin\xjc.bat players.xsd -p players
This will "compile" the XSD file for players.xml, create java classes, and place them into a directory name players
This will "compile" the XSD file for players.xml, create java classes, and place them into a directory named "players"


4. Compile the genereated java classes. Just create a "setup.bat" file with the following contents:
4. Compile the genereated java classes. Just create a "setup.bat" file with the following contents:
Line 16: Line 16:
  set JAXP_LIBS=C:\Sun\jwsdp-1.6\jaxp\lib
  set JAXP_LIBS=C:\Sun\jwsdp-1.6\jaxp\lib
  set JWSDP_LIBS=C:\Sun\jwsdp-1.6\jwsdp-shared\lib
  set JWSDP_LIBS=C:\Sun\jwsdp-1.6\jwsdp-shared\lib
  set LIBS=%JAXB_LIBS%\jaxb-api.jar;%JAXB_LIBS%\jaxb-impl.jar;%JAXB_LIBS%\jaxb-xjc.jar;%JAXB_LIBS%\jaxb-libs.jar;%JAXP_LIBS%\jaxb-api.jar;%JAXP_LIBS%\endorsed\xercesImpl.jar;
  set LIBS=%JAXB_LIBS%\jaxb-api.jar;%JAXB_LIBS%\jaxb-impl.jar;%JAXB_LIBS%\jaxb-xjc.jar;
%JAXP_LIBS%\endorsed\xalan.jar;%JAXP_LIBS%\endorsed\sax.jar;%JAXP_LIBS%\endorsed\dom.jar;%JWSDP_LIBS%\jax-qname.jar;%JWSDP_LIBS%\namespace.jar;
%JAXB_LIBS%\jaxb-libs.jar;%JAXP_LIBS%\jaxb-api.jar;%JAXP_LIBS%\endorsed\xercesImpl.jar;
%JAXP_LIBS%\endorsed\xalan.jar;%JAXP_LIBS%\endorsed\sax.jar;%JAXP_LIBS%\endorsed\dom.jar;
%JWSDP_LIBS%\jax-qname.jar;%JWSDP_LIBS%\namespace.jar;
  javac -classpath %LIBS%;. -d C:\myDir\ players\*.java players\impl\*.java
  javac -classpath %LIBS%;. -d C:\myDir\ players\*.java players\impl\*.java
The "set" command above was too long to write in one line on this wiki, in the the ".bat" file, you should write it in one line


5. Create a jar archive from the compiled classes
5. Create a jar archive from the compiled classes

Revision as of 17:13, 17 February 2006

Generating Java classes with JAXB

1. First create XSD files for the XML files that you need.
You can also create them automatically with different programs.

2. Install Java Web Services from http://java.sun.com/webservices/ - you need these libraries
Default installs to C:\Sun\

3. Create Java classes with the following command:

C:\Sun\jwsdp-1.6\jaxb\bin\xjc.bat players.xsd -p players

This will "compile" the XSD file for players.xml, create java classes, and place them into a directory named "players"

4. Compile the genereated java classes. Just create a "setup.bat" file with the following contents:

@echo off
set JAXB_LIBS=C:\Sun\jwsdp-1.6\jaxb\lib
set JAXP_LIBS=C:\Sun\jwsdp-1.6\jaxp\lib
set JWSDP_LIBS=C:\Sun\jwsdp-1.6\jwsdp-shared\lib
set LIBS=%JAXB_LIBS%\jaxb-api.jar;%JAXB_LIBS%\jaxb-impl.jar;%JAXB_LIBS%\jaxb-xjc.jar;
%JAXB_LIBS%\jaxb-libs.jar;%JAXP_LIBS%\jaxb-api.jar;%JAXP_LIBS%\endorsed\xercesImpl.jar;
%JAXP_LIBS%\endorsed\xalan.jar;%JAXP_LIBS%\endorsed\sax.jar;%JAXP_LIBS%\endorsed\dom.jar;
%JWSDP_LIBS%\jax-qname.jar;%JWSDP_LIBS%\namespace.jar;
javac -classpath %LIBS%;. -d C:\myDir\ players\*.java players\impl\*.java

The "set" command above was too long to write in one line on this wiki, in the the ".bat" file, you should write it in one line

5. Create a jar archive from the compiled classes

jar cvf players.jar players\*

All paths written above were relative.
(replace, for example, players\*.java with D:\yourDir\players\*.java or just leave them if they're in the current dir)

6. Include the generated jar archive in your project.
7. Using the classes:

 import players.HattrickDataType.TeamType.PlayerListType.*;
 //this is required for this example, for others, it may be something similar or nothing
 try {
   //create an object containing the information in the xml (unmarshall)
   JAXBContext jc = JAXBContext.newInstance("players");
   Unmarshaller u = jc.createUnmarshaller();
   u.setValidating(true);
   players.HattrickData c = (players.HattrickData)u.unmarshal(
            new FileInputStream("xml\\players.xml"));
   //play around with the object :)
   List p = c.getTeam().getPlayerList().getPlayer();
   for (obj : p) {
         String name = ((PlayerType)p.get(i)).getPlayerName();
         System.out.println(name);
   }
   //if you made changes to it, save them back to the XML (marshall)
   Marshaller m = jc.createMarshaller();
   m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
   m.marshal(c, new FileOutputStream("xml\\properties.xml"));
 } catch (Exception e) {
     e.printStackTrace();
 }
 //the end

8. Create a graphical interface and use all these.


Example XSDs:

players.xml

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xs:element name="HattrickData">
   <xs:complexType>
     <xs:sequence>
       <xs:element name="FileName" type="xs:string" />
       <xs:element name="Version" type="xs:string" />
       <xs:element name="UserID" type="xs:unsignedInt" />
       <xs:element name="FetchedDate" type="xs:string" />
       <xs:element name="ActionType" type="xs:string" />
       <xs:element name="Team">
         <xs:complexType>
           <xs:sequence>
             <xs:element name="TeamID" type="xs:unsignedInt" />
             <xs:element name="TeamName" type="xs:string" />
             <xs:element name="PlayerList">
               <xs:complexType>
                  <xs:sequence>
                   <xs:element maxOccurs="unbounded" name="Player">
                     <xs:complexType>
                       <xs:sequence>
                         <xs:element name="PlayerID" type="xs:unsignedInt" />
                         <xs:element name="PlayerName" type="xs:string" />
                         <xs:element name="PlayerNumber" type="xs:unsignedInt" />
                         <xs:element name="Age" type="xs:unsignedInt" />
                         <xs:element name="MarketValue" type="xs:unsignedInt" />
                         <xs:element name="TSI" type="xs:unsignedInt" />
                         <xs:element name="PlayerForm" type="xs:unsignedInt" />
                         <xs:element name="Statement" />
                         <xs:element name="Experience" type="xs:unsignedInt" />
                         <xs:element name="Leadership" type="xs:unsignedInt" />
                         <xs:element name="Salary" type="xs:unsignedInt" />
                         <xs:element name="IsAbroad" type="xs:unsignedInt" />
                         <xs:element name="Agreeability" type="xs:unsignedInt" />
                         <xs:element name="Aggressiveness" type="xs:unsignedInt" />
                         <xs:element name="Honesty" type="xs:unsignedInt" />
                         <xs:element name="LeagueGoals" type="xs:unsignedInt" />
                         <xs:element name="CupGoals" type="xs:unsignedInt" />
                         <xs:element name="FriendliesGoals" type="xs:unsignedInt" />
                         <xs:element name="CareerGoals" type="xs:unsignedInt" />
                         <xs:element name="CareerHattricks" type="xs:unsignedInt" />
                         <xs:element name="Specialty" type="xs:unsignedInt" />
                         <xs:element name="TransferListed" type="xs:unsignedInt" />
                         <xs:element name="NationalTeamID" type="xs:unsignedInt" />
                         <xs:element name="CountryID" type="xs:unsignedInt" />
                         <xs:element name="Caps" type="xs:unsignedInt" />
                         <xs:element name="CapsU20" type="xs:unsignedInt" />
                         <xs:element name="Cards" type="xs:unsignedInt" />
                         <xs:element name="InjuryLevel" type="xs:byte" />
                         <xs:element name="StaminaSkill" type="xs:unsignedInt" />
                         <xs:element name="KeeperSkill" type="xs:unsignedInt" />
                         <xs:element name="PlaymakerSkill" type="xs:unsignedInt" />
                         <xs:element name="ScorerSkill" type="xs:unsignedInt" />
                         <xs:element name="PassingSkill" type="xs:unsignedInt" />
                         <xs:element name="WingerSkill" type="xs:unsignedInt" />
                         <xs:element name="DefenderSkill" type="xs:unsignedInt" />
                         <xs:element name="SetPiecesSkill" type="xs:unsignedInt" />
                         <xs:element minOccurs="0" name="TrainerData">
                           <xs:complexType>
                             <xs:sequence>
                               <xs:element name="TrainerType" type="xs:unsignedInt" />
                               <xs:element name="TrainerSkill" type="xs:unsignedInt" />
                             </xs:sequence>
                           </xs:complexType>
                         </xs:element>
                       </xs:sequence>
                       <xs:attribute name="Index" type="xs:unsignedInt" use="required" />
                     </xs:complexType>
                   </xs:element>
                 </xs:sequence>
                 <xs:attribute name="Count" type="xs:unsignedInt" use="required" />
               </xs:complexType>
             </xs:element>
           </xs:sequence>
         </xs:complexType>
       </xs:element>
     </xs:sequence>
   </xs:complexType>
 </xs:element>
</xs:schema>

playerDetails.xml

<?xml version="1.0" encoding="utf-8"?>
<xs:schema attributeFormDefault="unqualified" elementFormDefault="qualified" xmlns:xs="http://www.w3.org/2001/XMLSchema">
 <xs:element name="HattrickData">
   <xs:complexType>
     <xs:sequence>
       <xs:element name="FileName" type="xs:string" />
       <xs:element name="Version" type="xs:decimal" />
       <xs:element name="UserID" type="xs:unsignedInt" />
       <xs:element name="UserIsSupporter" type="xs:unsignedInt" />
       <xs:element name="UserHasClubHouse" type="xs:unsignedInt" />
       <xs:element name="FetchedDate" type="xs:string" />
       <xs:element name="ActionType" type="xs:string" />
       <xs:element name="ActionSuccess" type="xs:string" />
       <xs:element name="Player">
         <xs:complexType>
           <xs:sequence>
             <xs:element name="PlayerID" type="xs:unsignedInt" />
             <xs:element name="PlayerName" type="xs:string" />
             <xs:element name="PlayerNumber">
               <xs:complexType>
                 <xs:attribute name="Available" type="xs:boolean" use="required" />
               </xs:complexType>
             </xs:element>
             <xs:element name="Age" type="xs:unsignedInt" />
             <xs:element name="PlayerForm" type="xs:unsignedInt" />
             <xs:element name="Cards">
               <xs:complexType>
                 <xs:simpleContent>
                   <xs:extension base="xs:unsignedInt">
                     <xs:attribute name="Available" type="xs:boolean" use="required" />
                   </xs:extension>
                 </xs:simpleContent>
               </xs:complexType>
             </xs:element>
             <xs:element name="InjuryLevel">
               <xs:complexType>
                 <xs:simpleContent>
                   <xs:extension base="xs:unsignedInt">
                     <xs:attribute name="Available" type="xs:boolean" use="required" />
                   </xs:extension>
                 </xs:simpleContent>
               </xs:complexType>
             </xs:element>
             <xs:element name="Statement">
               <xs:complexType>
                 <xs:attribute name="Available" type="xs:boolean" use="required" />
               </xs:complexType>
             </xs:element>
             <xs:element name="PlayerLanguage">
               <xs:complexType>
                 <xs:attribute name="Available" type="xs:boolean" use="required" />
               </xs:complexType>
             </xs:element>
             <xs:element name="TrainerData">
               <xs:complexType>
                 <xs:sequence>
                   <xs:element name="TrainerType" type="xs:unsignedInt" />
                   <xs:element name="TrainerSkill" type="xs:unsignedInt" />
                 </xs:sequence>
                 <xs:attribute name="Available" type="xs:boolean" use="required" />
               </xs:complexType>
             </xs:element>
             <xs:element name="Agreeability" type="xs:unsignedInt" />
             <xs:element name="Aggressiveness" type="xs:unsignedInt" />
             <xs:element name="Honesty" type="xs:unsignedInt" />
             <xs:element name="Experience" type="xs:unsignedInt" />
             <xs:element name="Leadership" type="xs:unsignedInt" />
             <xs:element name="Specialty" type="xs:unsignedInt" />
             <xs:element name="NativeLeagueID" type="xs:unsignedInt" />
             <xs:element name="NativeLeagueName" type="xs:string" />
             <xs:element name="TSI" type="xs:unsignedInt" />
             <xs:element name="OwningTeam">
               <xs:complexType>
                 <xs:sequence>
                   <xs:element name="TeamID" type="xs:unsignedInt" />
                   <xs:element name="TeamName" type="xs:string" />
                   <xs:element name="LeagueID" type="xs:unsignedInt" />
                 </xs:sequence>
                 <xs:attribute name="Available" type="xs:boolean" use="required" />
               </xs:complexType>
             </xs:element>
             <xs:element name="Salary" type="xs:unsignedShort" />
             <xs:element name="IsAbroad" type="xs:unsignedInt" />
             <xs:element name="PlayerSkills">
               <xs:complexType>
                 <xs:sequence>
                   <xs:element name="StaminaSkill" type="xs:unsignedInt" />
                   <xs:element name="KeeperSkill" type="xs:unsignedInt" />
                   <xs:element name="PlaymakerSkill" type="xs:unsignedInt" />
                   <xs:element name="ScorerSkill" type="xs:unsignedInt" />
                   <xs:element name="PassingSkill" type="xs:unsignedInt" />
                   <xs:element name="WingerSkill" type="xs:unsignedInt" />
                   <xs:element name="DefenderSkill" type="xs:unsignedInt" />
                   <xs:element name="SetPiecesSkill" type="xs:unsignedInt" />
                 </xs:sequence>
                 <xs:attribute name="Available" type="xs:boolean" use="required" />
               </xs:complexType>
             </xs:element>
             <xs:element name="Caps" type="xs:unsignedInt" />
             <xs:element name="CapsU20" type="xs:unsignedInt" />
             <xs:element name="CareerGoals">
               <xs:complexType>
                 <xs:simpleContent>
                   <xs:extension base="xs:unsignedInt">
                     <xs:attribute name="Available" type="xs:boolean" use="required" />
                   </xs:extension>
                 </xs:simpleContent>
               </xs:complexType>
             </xs:element>
             <xs:element name="CareerHattricks">
               <xs:complexType>
                 <xs:simpleContent>
                   <xs:extension base="xs:unsignedInt">
                     <xs:attribute name="Available" type="xs:boolean" use="required" />
                   </xs:extension>
                 </xs:simpleContent>
               </xs:complexType>
             </xs:element>
             <xs:element name="LeagueGoals">
               <xs:complexType>
                 <xs:simpleContent>
                   <xs:extension base="xs:unsignedInt">
                     <xs:attribute name="Available" type="xs:boolean" use="required" />
                   </xs:extension>
                 </xs:simpleContent>
               </xs:complexType>
             </xs:element>
             <xs:element name="CupGoals">
               <xs:complexType>
                 <xs:simpleContent>
                   <xs:extension base="xs:unsignedInt">
                     <xs:attribute name="Available" type="xs:boolean" use="required" />
                   </xs:extension>
                 </xs:simpleContent>
               </xs:complexType>
             </xs:element>
             <xs:element name="TransferListed" type="xs:unsignedInt" />
           </xs:sequence>
         </xs:complexType>
       </xs:element>
     </xs:sequence>
   </xs:complexType>
 </xs:element>
</xs:schema>