如何使用 JAXB2 将 XML 与动态元素编组



我正在开发一个SOAP客户端,我正在寻找更复杂的解决方案,使用Jaxb2库将对象编组成XML字符串。

目标是编组一个对象,该对象充当任何类型元素的包装器。例:

<Action id="5">
<Employee id="10">
<Name>John</Name>
</Employee>
</Action>

或。

<Action id="5">
<Department id="ABC">
<Name>Economy Department</Name>
<ParentId>CDE</ParentId>
</Department>
</Action>

注意:xml 根(操作)包含"员工"或"部门"或其他任何内容。

我目前的工作解决方案如下:

@XmlRootElement(name = "Action")
abstract class Action {
@XmlAttribute(name = "id")
protected String id;
}
class EmployeeAction extends Action {

@XmlElement(name = "Employee")
protected Employee employee;
}
class DepartmentAction extends Action {

@XmlElement(name = "Department")
protected Department department;
}

这工作正常,但我正在寻找更通用的解决方案,而无需为每种类型创建类(*操作扩展操作)。元素的名称必须始终与(动态)类型的类名相同。我的想法是这样的:

public class Action<T> {
@XmlAttribute(name = "id")
protected String id;
@XmlElement(name = "getClass().getSimpleName()") //???
protected T element;
}

。并编组如下内容:

Action<?> action = ...;
JAXBContext context = JAXBContext.newInstance(Action.class, action.getElement().getClass());
Marshaller marshaller = context.createMarshaller();
try(ByteArrayOutputStream outStream = new ByteArrayOutputStream()) {
marshaller.marshal(action, outStream);
return outStream.toString();
}

这样的事情可能吗?

提前谢谢。

您可以对上面提供的 XML 执行以下操作:

方法-1

行动.class;

@XmlRootElement(name = "Action")
@Data
@XmlAccessorType(XmlAccessType.FIELD)
public class Action {
@XmlAttribute(name = "id")
protected String id;
@XmlElements({
@XmlElement(name = "Employee", type = Employee.class),
@XmlElement(name = "Department", type = Department .class),
})
private ActionItem type;
}

行动项.class;

@Data
@XmlAccessorType(XmlAccessType.NONE)
public class ActionItem {
@XmlElement(name = "Name")
protected String name;
}

员工.class;

@Data
@XmlAccessorType(XmlAccessType.NONE)
public class Employee extends ActionItem {
@XmlAttribute(name = "id")
private String id;
}

部门.class;

@Data
@XmlAccessorType(XmlAccessType.NONE)
public class Department extends ActionItem {
@XmlAttribute
private String id;
@XmlElement(name = "ParentId")
private String parentID;
}

主.class:


public class Main {
public static void main(String[] args) throws JAXBException, XMLStreamException {
final InputStream inputStream = Unmarshalling.class.getClassLoader().getResourceAsStream("action.xml");
final XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
final Unmarshaller unmarshaller = JAXBContext.newInstance(Action.class).createUnmarshaller();
final Action action = unmarshaller.unmarshal(xmlStreamReader, Action.class).getValue();
System.out.println(action.toString());
Marshaller marshaller = JAXBContext.newInstance(Action.class).createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(action, System.out);
}
}

如果您提供了EmployeeXML,则会产生以下结果:

Action(id=5, type=Employee(id=10))
<Action id="5">
<Employee id="10">
<Name>John</Name>
</Employee>
</Action>

如果提供DepartmentXML,则会产生以下结果:

Action(id=5, type=Department(parentID=CDE))
<Action id="5">
<Department>
<Name>Economy Department</Name>
<ParentId>CDE</ParentId>
</Department>
</Action>

方法-2

创建接口并使用它:

public interface ActionItem2 {
}

操作.class使用创建的接口。

@XmlRootElement(name = "Action")
@Data
@XmlAccessorType(XmlAccessType.FIELD)
public class Action {
@XmlAttribute(name = "id")
protected String id;
@XmlElements({
@XmlElement(name = "Employee", type = Employee.class),
@XmlElement(name = "Department", type = Department .class),
})
private ActionItem2 type;
}

员工.class实现创建的接口

@Data
@XmlAccessorType(XmlAccessType.NONE)
public class Employee implements ActionItem2 {
@XmlAttribute(name = "id")
private String id;
@XmlElement(name = "Name")
protected String name;
}

部门.class实现创建的接口

@Data
@XmlAccessorType(XmlAccessType.NONE)
public class Department implements ActionItem2 {
@XmlAttribute
private String id;
@XmlElement(name = "ParentId")
private String parentID;
@XmlElement(name = "Name")
protected String name;
}

主.class(无变化)

public class Main {
public static void main(String[] args) throws JAXBException, XMLStreamException {
final InputStream inputStream = Unmarshalling.class.getClassLoader().getResourceAsStream("action.xml");
final XMLStreamReader xmlStreamReader = XMLInputFactory.newInstance().createXMLStreamReader(inputStream);
final Unmarshaller unmarshaller = JAXBContext.newInstance(Action.class).createUnmarshaller();
final Action action = unmarshaller.unmarshal(xmlStreamReader, Action.class).getValue();
System.out.println(action.toString());
Marshaller marshaller = JAXBContext.newInstance(Action.class).createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
marshaller.marshal(action, System.out);
}
}

结果是一样的。

方法 - 3

如果你不想修改你的POJO,那么你可以做这样的事情:

@XmlRootElement(name = "Action")
@Data
@XmlAccessorType(XmlAccessType.FIELD)
public class Action {
@XmlAttribute(name = "id")
protected String id;
@XmlElements({
@XmlElement(name = "Employee", type = Employee.class),
@XmlElement(name = "Department", type = Department .class),
})
private Object type;
}

员工.class:

@Data
@XmlAccessorType(XmlAccessType.NONE)
public class Employee {
@XmlAttribute(name = "id")
private String id;
@XmlElement(name = "Name")
protected String name;
}

部门.class:


@Data
@XmlAccessorType(XmlAccessType.NONE)
public class Department {
@XmlAttribute
private String id;
@XmlElement(name = "ParentId")
private String parentID;
@XmlElement(name = "Name")
protected String name;
}

这将提供相同的输出。

最新更新