Java 将具有相同标记的xml结构解组到不同字段
我必须阅读和编写XML文档,如:Java 将具有相同标记的xml结构解组到不同字段,java,jaxb,Java,Jaxb,我必须阅读和编写XML文档,如: <connections> <connection> <operation>add</operation> <node>A01</node> <interface>AIF01</interface> <node>B01</node> <interface>BIF01</interfa
<connections>
<connection>
<operation>add</operation>
<node>A01</node>
<interface>AIF01</interface>
<node>B01</node>
<interface>BIF01</interface>
</connection>
<connection>
<operation>delete</operation>
<node>A02</node>
<interface>AIF02</interface>
<node>B02</node>
<interface>BIF02</interface>
</connection>
因此,当我解组这样一个xml文件时,targetNode
和targetInterface
为空,因为它映射到source*
字段
有人能帮忙吗?
提前谢谢。您提到编组工作很好。。。但是如果有两个属性映射到一个XmlElement。。。哪一个正在使用?它是否可以正常工作,因为在这两种情况下都对源*字段进行编组和反编组?在任何情况下,您可能都可以使用事件处理程序方法,如
afterUnmarshall
,来完成您所期望的任务。因此sourceNode
和targetNode
使用相同的标记
。这种方法工作得很好,但是可以肯定,如果解组需要另一个实现,我必须更改实现,以便解组和编组工作。但怎么办?:-)
@XmlElement(name = "connection")
List<Connection> connections;
@XmlAccessorType(XmlAccessType.FIELD)
public class Connection {
@XmlElement(name = "operation")
private Operation operation;
@XmlElement(name = "node")
private String sourceNode;
@XmlElement(name = "interface")
private String sourceInterface;
@XmlElement(name = "node")
private String targetNode;
@XmlElement(name = "interface")
private String targetInterface;
}