java - xml 未填充 - 使用 JAXB

标签 java xml binding jaxb javax.xml

大家晚上好。我在学校实验室方面遇到了一些问题。本质上,我必须创建一个 XML 架构,运行 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 中定义的员工。任何形式的建议或帮助将不胜感激。

提前非常感谢您。

干杯

最佳答案

你可以这样做

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>

关于java - xml 未填充 - 使用 JAXB,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21927249/

相关文章:

Java,从桌面应用程序迁移到 Web 应用程序

java - 从 org.w3c.dom.Node 获取 Xpath

java - 如何从列表 <String> 中删除换行符

java - 读取标签之间的 XML 数据

WPF 绑定(bind) : Add Button to Unbound Field in grid

c# - 我可以将剪贴板作为命令参数发送吗?

java - spring事务注释方法无需对实体进行合并调用

java - 从android中的文本文件读取

java - 从属性中删除元素

c# - WPF 绑定(bind)到变量/DependencyProperty