2016-01-25 56 views
0

我想使用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> 

預先感謝您的任何見解

+0

你的XML的結構是不是XML模式(在多個位置EncryptedData元素多次)表達。因此,使用JAXB註釋來表達這一點是不可能的。您可以嘗試使用XSLT並進行替換,或者創建文檔使用DOM –

回答

0

你只想要更改的元素=的EncryptedData,即保持它們的相對位置不變的EncryptedData元素?

如果是這樣的話,它可能會使用JAXB粘合劑可能, 看到JAXB & XML Infoset Preservation

+0

太棒了!按照鏈接文章中的建議,我從默認的JAXB實現切換到MOXy後,這種方式起作用了,唯一的小問題是由於某種原因,它會去掉xmlns屬性的值,但我會在之後將它添加回去。感謝您的幫助! – snowcloned