大家晚上好。我在學校的實驗室遇到了一些問題。實質上,我必須創建一個XML Schema,運行JAXB編譯器,然後創建Java類來序列化en員工記錄。xml沒有填充 - 使用JAXB
所以這是我到目前爲止有:被稱爲「employeeSchema.xsd」
<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
xmlns:jxb="http://java.sun.com/xml/ns/jaxb"
jxb:version="2.0">
<xs:element name="employee" type="employeeType"/>
<xs:complexType name="jobListType">
<xs:sequence>
<xs:element name="job_id" type="xs:positiveInteger"/>
<xs:element name="job_title" type="xs:string"/>
<xs:element name="min_salary" type="xs:positiveInteger"/>
<xs:element name="max_salary" type="xs:positiveInteger"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="departmentType">
<xs:sequence>
<xs:element name="department_id" type="xs:positiveInteger"/>
<xs:element name="department_name" type="xs:string"/>
<xs:element name="manager_id" type="xs:positiveInteger"/>
<xs:element name="location_id" type="xs:positiveInteger"/>
</xs:sequence>
</xs:complexType>
<xs:complexType name="employeeType">
<xs:sequence>
<xs:element name="employee_id" type="xs:positiveInteger"/>
<xs:element name="first_name" type="xs:string"/>
<xs:element name="last_name" type="xs:string"/>
<xs:element name="email" type="xs:string"/>
<xs:element name="phone_number" type="xs:string"/>
<xs:element name="hire_date" type="xs:date"/>
<xs:element name="job" type="jobListType"/>
<xs:element name="salary" type="xs:positiveInteger"/>
<xs:element name="commission_pct" type="xs:decimal"/>
<xs:element name="manager_id" type="xs:positiveInteger"/>
<xs:element name="department" type="departmentType"/>
</xs:sequence>
</xs:complexType>
</xs:schema>
一個文件,一旦我把它用JAXB遵守,我創造了這個班叫做「employee.java」
package employeeSchema;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.xml.bind.*;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;
import java.math.*;
public class employee {
private ObjectFactory of;
private EmployeeType myEmployee;
public employee(){
of = new ObjectFactory();
myEmployee = of.createEmployeeType();
}
public void make(BigInteger empID, String firstName, String lastName, String email, String phoneNumber,
Date hireDate, JobListType jobID, BigInteger salary, BigDecimal commPct,
BigInteger managerID/*, DepartmentType departmentID*/){
try{
GregorianCalendar gcal = new GregorianCalendar();
gcal.setTime(hireDate);
XMLGregorianCalendar xgcal = DatatypeFactory.newInstance().newXMLGregorianCalendar(gcal);
EmployeeType emp = of.createEmployeeType();
emp.setEmployeeId(empID);
emp.setFirstName(firstName);
emp.setLastName(lastName);
emp.setEmail(email);
emp.setPhoneNumber(phoneNumber);
emp.setHireDate(xgcal);
emp.setJob(jobID);
emp.setSalary(salary);
emp.setCommissionPct(commPct);
emp.setManagerId(managerID);
}
catch (Exception e){
}
}
public void marshal() {
try {
JAXBElement<EmployeeType> em =
of.createEmployee(myEmployee);
JAXBContext jc = JAXBContext.newInstance("employeeSchema");
Marshaller m = jc.createMarshaller();
m.marshal(em, System.out);
} catch(JAXBException jbe){
// ...
}
}
public static void main(String args[])
{
int employeeID = 123456;
BigInteger empID = BigInteger.valueOf(employeeID);
String firstName = "Ehssan";
String lastName = "Tehrani";
String email = "[email protected]";
Date hireDate = null;
try{
DateFormat df=new SimpleDateFormat("dd/MM/yyyy");
hireDate=df.parse("20/02/2014");
}
catch (Exception e){
}
String phoneNumber = "647-588-3774";
JobListType jList = new JobListType();
int theJobId = 12345;
BigInteger jID = BigInteger.valueOf(theJobId);
jList.setJobId(jID);
jList.setJobTitle("Java Developer");
int theMinSal = 50000;
BigInteger jMinSal = BigInteger.valueOf(theMinSal);
jList.setMinSalary(jMinSal);
int theMaxSal = 150000;
BigInteger jMaxSal = BigInteger.valueOf(theMaxSal);
jList.setMinSalary(jMaxSal);
int theSalary = 90000;
BigInteger salary = BigInteger.valueOf(theSalary);
BigDecimal commPct = new BigDecimal(5.0);
int theManagerID = 12345;
BigInteger managerID = BigInteger.valueOf(theManagerID);
//DepartmentType departmentID
//setDepartmentID
employee myEmp = new employee();
myEmp.make(empID, firstName, lastName, email, phoneNumber, hireDate, jList, salary, commPct, managerID);
myEmp.marshal();
}
}
然而,當我運行employee.java,我的輸出如下:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><employee/>
但目的是爲了顯示主要定義的員工。任何形式的建議或幫助將不勝感激。
非常感謝您提前。
乾杯
具有u使用XJC命令從XSD生成Java類? – Naren
@Naren:是的,我創建XSD後,我做了以下幾點:jxc -p employeeSchema employeeSchema.xsd ...它編寫得很好,並創建了OjbectFactory和相關文件。 –