簡體   English   中英

xml不填充-使用JAXB

[英]xml not populating - using JAXB

各位晚上好。 我的學校實驗室有點問題。 本質上,我必須創建一個XML Schema,運行JAXB編譯器,然后創建java類來序列化員工記錄。

因此,這就是我到目前為止所擁有的:一個名為“ 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/>

但是目的是要顯示我在主體中定義的員工。 任何形式的建議或幫助將不勝感激。

提前非常感謝您。

干杯

你可以這樣

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>

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM