xml没有填充 - 使用JAXB

时间:2014-02-21 06:47:38

标签: java xml binding jaxb javax.xml

大家晚上好。我在学校实验室遇到了一些问题。本质上,我必须创建一个XML Schema,运行JAXB编译器,然后创建java类来序列化一个en员工记录。

所以这就是我到目前为止:一个名为“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 = "etehrani@mysenecac.ca";

    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/>

但目的是显示我在main中定义的员工。任何建议或帮助将不胜感激。

非常感谢你。

干杯

1 个答案:

答案 0 :(得分:2)

你可以这样做

package generated;

import java.math.BigDecimal;
import java.math.BigInteger;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.GregorianCalendar;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.datatype.DatatypeConfigurationException;
import javax.xml.datatype.DatatypeFactory;
import javax.xml.datatype.XMLGregorianCalendar;

public class Test {

    public static void main(String[] args) throws DatatypeConfigurationException {


        int employeeID = 123456;        
        BigInteger empID = BigInteger.valueOf(employeeID);


        String firstName = "Ehssan";
        String lastName = "Tehrani";
        String email = "etehrani@mysenecac.ca";

        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 departmentType=new DepartmentType();
        int theDepId = 1001;
        BigInteger theDepIdb = BigInteger.valueOf(theDepId);
        departmentType.setDepartmentId(theDepIdb);
        int theLocId = 1001;
        BigInteger theLocIdb = BigInteger.valueOf(theLocId);
        departmentType.setLocationId(theLocIdb);
        int theMagId = 1001;
        BigInteger theMagIdb = BigInteger.valueOf(theDepId);
        departmentType.setManagerId(theMagIdb);
        departmentType.setDepartmentName("tsetDepmName");

    //DepartmentType departmentID
        //setDepartmentID
        GregorianCalendar gcal = new GregorianCalendar();
        gcal.setTime(hireDate);
        XMLGregorianCalendar xgcal = DatatypeFactory.newInstance().newXMLGregorianCalendar(gcal);
      ObjectFactory factory=new ObjectFactory();

      EmployeeType emp=factory.createEmployeeType();
      emp.setJob(jList);
      emp.setDepartment(departmentType);

      emp.setEmployeeId(empID);
      emp.setFirstName(firstName);
      emp.setLastName(lastName);
      emp.setEmail(email);
      emp.setPhoneNumber(phoneNumber);
      emp.setHireDate(xgcal);

      emp.setSalary(salary);
      emp.setCommissionPct(commPct);
      emp.setManagerId(managerID);
      JAXBElement<EmployeeType> temp=factory.createEmployee(emp);

      try {


        JAXBContext jaxbContext = JAXBContext.newInstance(EmployeeType.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        // output pretty printed
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);


        jaxbMarshaller.marshal(temp, System.out);

          } catch (JAXBException e) {
        e.printStackTrace();
          }


    }

}

如果不是您的风格,请检查此

package generated;

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 EmployeeType make(BigInteger empID, String firstName, String lastName, String email, String phoneNumber, 
        Date hireDate, JobListType jobID, BigInteger salary, BigDecimal commPct, 
        BigInteger managerID/*, DepartmentType departmentID*/){
    EmployeeType emp=null;
    try{
        GregorianCalendar gcal = new GregorianCalendar();
        gcal.setTime(hireDate);
        XMLGregorianCalendar xgcal = DatatypeFactory.newInstance().newXMLGregorianCalendar(gcal);


         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);


        DepartmentType departmentType=new DepartmentType();
        int theDepId = 1001;
        BigInteger theDepIdb = BigInteger.valueOf(theDepId);
        departmentType.setDepartmentId(theDepIdb);
        int theLocId = 1001;
        BigInteger theLocIdb = BigInteger.valueOf(theLocId);
        departmentType.setLocationId(theLocIdb);
        int theMagId = 1001;
        BigInteger theMagIdb = BigInteger.valueOf(theDepId);
        departmentType.setManagerId(theMagIdb);
        departmentType.setDepartmentName("tsetDepmName");

        emp.setDepartment(departmentType);

    }

    catch (Exception e){
    }   

    return emp;
}

public void marshal(JAXBElement<EmployeeType> employeeType) {
    try {


        JAXBContext jaxbContext = JAXBContext.newInstance(EmployeeType.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

        // output pretty printed
        jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);


        jaxbMarshaller.marshal(employeeType, 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 = "etehrani@mysenecac.ca";

    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();

    EmployeeType temp=myEmp.make(empID, firstName, lastName, email, phoneNumber, hireDate, jList, salary, commPct, managerID);

    ObjectFactory factory=new ObjectFactory();
    JAXBElement<EmployeeType> test=factory.createEmployee(temp);
    myEmp.marshal(test);
}
}

输出是: -

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<employee>
    <employee_id>123456</employee_id>
    <first_name>Ehssan</first_name>
    <last_name>Tehrani</last_name>
    <email>etehrani@mysenecac.ca</email>
    <phone_number>647-588-3774</phone_number>
    <hire_date>2014-02-20+05:30</hire_date>
    <job>
        <job_id>12345</job_id>
        <job_title>Java Developer</job_title>
        <min_salary>150000</min_salary>
    </job>
    <salary>90000</salary>
    <commission_pct>5</commission_pct>
    <manager_id>12345</manager_id>
    <department>
        <department_id>1001</department_id>
        <department_name>tsetDepmName</department_name>
        <manager_id>1001</manager_id>
        <location_id>1001</location_id>
    </department>
</employee>