我的 XML 输出应该是这样的吗?
Is my XML output supposed to be like this?
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<staffs>
<records xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="employee">
<id>1</id>
<name>Danial</name>
<age>20</age>
<TFN>1231231</TFN>
<contact>12314123</contact>
<jobType>WAITER</jobType>
<wage>12.0</wage>
</records>
<records xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="employee">
<id>2</id>
<name>Andrew</name>
<age>21</age>
<TFN>1231254</TFN>
<contact>123677</contact>
<jobType>ADMIN</jobType>
<wage>18.0</wage>
</records>
<records xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="employee">
<id>3</id>
<name>Jenny</name>
<age>19</age>
<TFN>1231432</TFN>
<contact>1239873</contact>
<jobType>WAITRESS</jobType>
<wage>15.0</wage>
</records>
<employees>
<id>1</id>
<name>Danial</name>
<age>20</age>
<TFN>1231231</TFN>
<contact>12314123</contact>
<jobType>WAITER</jobType>
<wage>12.0</wage>
</employees>
<employees>
<id>2</id>
<name>Andrew</name>
<age>21</age>
<TFN>1231254</TFN>
<contact>123677</contact>
<jobType>ADMIN</jobType>
<wage>18.0</wage>
</employees>
<employees>
<id>3</id>
<name>Jenny</name>
<age>19</age>
<TFN>1231432</TFN>
<contact>1239873</contact>
<jobType>WAITRESS</jobType>
<wage>15.0</wage>
</employees>
</staffs>
无论如何,在使用 jaxb 之后,我的输出就是上面的内容。
这正常吗??为什么要重复输入?
Records 持有 LinkedList,Employees 扩展 Records
如果有什么办法可以防止这种情况发生,我们很想听听
import javax.xml.bind.annotation.*;
@XmlRootElement(name = "staffs")
@XmlAccessorType(XmlAccessType.FIELD)
public class Employees extends Records
{
@XmlElement(type = Employee.class)
public LinkedList<Record> getEmployees()
{
return super.getRecords();
}
}
//////////////////////Employee class/////////////////
import javax.xml.bind.annotation.*;
@XmlRootElement(name = "staff")
@XmlAccessorType(XmlAccessType.FIELD)
public class Employee extends Record
{
}
问题是因为您必须 getter 实际上 return 相同的列表:
- Record.getRecords() 和
- Employee.getEmployees().
见
public LinkedList<Record> getEmployees()
{
return super.getRecords();
}
这就是为什么你有这两个几乎相同的列表。你真的需要这个副本 getter 吗?如果没有,只需将其删除,输出将没有重复项。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<staffs>
<records xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="employee">
<id>1</id>
<name>Danial</name>
<age>20</age>
<TFN>1231231</TFN>
<contact>12314123</contact>
<jobType>WAITER</jobType>
<wage>12.0</wage>
</records>
<records xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="employee">
<id>2</id>
<name>Andrew</name>
<age>21</age>
<TFN>1231254</TFN>
<contact>123677</contact>
<jobType>ADMIN</jobType>
<wage>18.0</wage>
</records>
<records xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="employee">
<id>3</id>
<name>Jenny</name>
<age>19</age>
<TFN>1231432</TFN>
<contact>1239873</contact>
<jobType>WAITRESS</jobType>
<wage>15.0</wage>
</records>
<employees>
<id>1</id>
<name>Danial</name>
<age>20</age>
<TFN>1231231</TFN>
<contact>12314123</contact>
<jobType>WAITER</jobType>
<wage>12.0</wage>
</employees>
<employees>
<id>2</id>
<name>Andrew</name>
<age>21</age>
<TFN>1231254</TFN>
<contact>123677</contact>
<jobType>ADMIN</jobType>
<wage>18.0</wage>
</employees>
<employees>
<id>3</id>
<name>Jenny</name>
<age>19</age>
<TFN>1231432</TFN>
<contact>1239873</contact>
<jobType>WAITRESS</jobType>
<wage>15.0</wage>
</employees>
</staffs>
无论如何,在使用 jaxb 之后,我的输出就是上面的内容。 这正常吗??为什么要重复输入? Records 持有 LinkedList,Employees 扩展 Records
如果有什么办法可以防止这种情况发生,我们很想听听
import javax.xml.bind.annotation.*;
@XmlRootElement(name = "staffs")
@XmlAccessorType(XmlAccessType.FIELD)
public class Employees extends Records
{
@XmlElement(type = Employee.class)
public LinkedList<Record> getEmployees()
{
return super.getRecords();
}
}
//////////////////////Employee class/////////////////
import javax.xml.bind.annotation.*;
@XmlRootElement(name = "staff")
@XmlAccessorType(XmlAccessType.FIELD)
public class Employee extends Record
{
}
问题是因为您必须 getter 实际上 return 相同的列表:
- Record.getRecords() 和
- Employee.getEmployees().
见
public LinkedList<Record> getEmployees()
{
return super.getRecords();
}
这就是为什么你有这两个几乎相同的列表。你真的需要这个副本 getter 吗?如果没有,只需将其删除,输出将没有重复项。