我想使用JAXB解组一个元素出现5次,但不在一行中的XML文件;我想进行一些更改,然后将其编组回到XML。当回写到XML文件时,元素的实例需要按照相同的顺序返回,并且使用与之前相同的中间元素进行分隔JAXB unmarshalling - 出现多次由其他元素分隔的元素
我知道我可以用一个Collection代表多次出现的元素,我可以使用@XmlType(propOrder = {...})来指定字段的顺序,但我无法弄清楚两者是同时进行的......
我试过使用5个不同的字段名在我的Java类中(encryptedData1,encryptedData2,...)和5个不同的getter/setter对,然后使用相同名称注释setter:
@XmlElement(name = "EncryptedData")
但是当我解组时,只有第一个被设置,其他的都是空的。填充的字段具有XML文件中最后一个实例的值,所以我猜测它只是设置了五次
如果我使用List,那么当我写出XML文件时,它们全部写在一起
以下是原始XML的示例; EncryptedData元素是一个问题:
<NodeInformation>
...
<ProxyIpPort>1194</ProxyIpPort>
<LicenseKeyID />
<EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element" xmlns="http://www.w3.org/2001/04/xmlenc#">
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
<CipherData>
<CipherValue>************************</CipherValue>
</CipherData>
</EncryptedData>
<ProxyUsername />
<EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element" xmlns="http://www.w3.org/2001/04/xmlenc#">
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
<CipherData>
<CipherValue>***********************</CipherValue>
</CipherData>
</EncryptedData>
<ActualIpAddress />
<HasMASServer>false</HasMASServer>
<MASServerIpAddress />
<MASServerWebListeningPort>443</MASServerWebListeningPort>
<ModemNumber />
<RememberLoginPassword>true</RememberLoginPassword>
<LoginName>admin</LoginName>
<EncryptedData Type="http://www.w3.org/2001/04/xmlenc#Element" xmlns="http://www.w3.org/2001/04/xmlenc#">
<EncryptionMethod Algorithm="http://www.w3.org/2001/04/xmlenc#tripledes-cbc" />
<CipherData>
<CipherValue>***************************</CipherValue>
</CipherData>
</EncryptedData>
...
</NodeInformation>
预先感谢您的任何见解
你的XML的结构是不是XML模式(在多个位置EncryptedData元素多次)表达。因此,使用JAXB注释来表达这一点是不可能的。您可以尝试使用XSLT并进行替换,或者创建文档使用DOM –