我有一个需要生成XML的场景,如下所示-
<EVC_unidetailcontainer>
<EVC_unidetail>
<UREF value="01" />
<UACT value="N" />
<NCI value="02VLN.A2" />
<EVCSP value="HSTQTX0211W" />
<S_VLANdetailcontainer>
<S_VLANdetail>
<S_VLAN value="1520" />
</S_VLANdetail>
</S_VLANdetailcontainer>
<RUID value="28.KGFD.000001..TWCC" />
<EVC_servicedetailcontainer>
<EVC_servicedetail>
<LREF value="1" />
<LOSACT value="N" />
<LOS value="BASIC" />
<BDW value="1G" />
</EVC_servicedetail>
</EVC_servicedetailcontainer>
</EVC_unidetail>
<EVC_unidetail>
<UREF value="02" />
<AUNT value="A" />
<UACT value="N" />
<NCI value="02VLN.A2" />
<S_VLANdetailcontainer>
<S_VLANdetail>
<S_VLAN value="1520" />
</S_VLANdetail>
</S_VLANdetailcontainer>
<EVC_servicedetailcontainer>
<EVC_servicedetail>
<LREF value="1" />
<LOSACT value="N" />
<LOS value="BASIC" />
<BDW value="1G" />
</EVC_servicedetail>
</EVC_servicedetailcontainer>
</EVC_unidetail>
我使用以下XMLAdapter来实现这种格式-
public class EVCUniDetailListAdapter extends XmlAdapter<EVCUniDetail, List<EVCUniDetail>>{
@Override
public EVCUniDetail marshal(List<EVCUniDetail> v) throws Exception {
EVCUniDetail evcUniDetailObj = new EVCUniDetail();
for (EVCUniDetail evcUniDetail : v) {
evcUniDetailObj.setEvcServiceDetailContainer(evcUniDetail.getEvcServiceDetailContainer());
evcUniDetailObj.setEVCSP(evcUniDetail.getEVCSP());
evcUniDetailObj.setNCI(evcUniDetail.getNCI());
evcUniDetailObj.setRUID(evcUniDetail.getRUID());
evcUniDetailObj.setsVLANDetailContainer(evcUniDetail.getsVLANDetailContainer());
evcUniDetailObj.setUACT(evcUniDetail.getUACT());
evcUniDetailObj.setUREF(evcUniDetail.getUREF());
}
return evcUniDetailObj;
}
@Override
public List<EVCUniDetail> unmarshal(EVCUniDetail v) throws Exception {
List<EVCUniDetail> evcUniDetailList = new ArrayList<>();
for (EVCUniDetail evcUniDetail : evcUniDetailList) {
evcUniDetailList.add(evcUniDetail);
}
return evcUniDetailList;
}
}
以上代码无法创建列表,并且在XML输出中仅返回 EVCUniDetail 的单个对象。
evcUniDetailList.add(evcUniDetail1); evcUniDetailList.add(evcUniDetail2);
我期望在XML中有两个 EVCUniDetail 对象,因为我在上面添加了两个对象。
这是我得到的以下输出-
<EVC_unidetailcontainer>
<EVC_unidetail>
<UREF value="01"/>
<UACT value="N"/>
<NCI value="02VLN.A2"/>
<EVCSP value="HSTQTX0211W"/>
<S_VLANdetailcontainer>
<S_VLANdetail>
<S_VLAN value="1520"/>
</S_VLANdetail>
</S_VLANdetailcontainer>
<RUID value="28.KGFD.000001..TWCC"/>
<EVC_servicedetailcontainer>
<EVC_servicedetail>
<LREF value="1"/>
<LOSACT value="N"/>
<LOS value="BASIC"/>
<BDW value="1G"/>
</EVC_servicedetail>
</EVC_servicedetailcontainer>
</EVC_unidetail>
以下是下面的编组代码-
JAXBContext context;
try {
context = JAXBContext.newInstance(new Class[]{asrOrder.getClass()});
Marshaller marshaller = context.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
marshaller.setProperty("com.sun.xml.internal.bind.xmlHeaders", "<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
marshaller.marshal(asrOrder, System.out);
} catch (JAXBException e) {
e.printStackTrace();
}
asrOrder 是具有多个并行对象的根对象。 EVC_unidetailcontainer是其中之一,其中包含EVC_unidetail的列表。
任何人都请指出错误所在。我看着已经问过的几个类似问题,但没有一个起作用。我认为编组代码中存在一些问题,但无法弄清楚。谢谢!!