下面是我的代码,它将返回一个字符串
这是我将xml转换为对象,将对象转换为map并再次映射到xml的所有代码。但是问题是我没有获得确切的xml格式,您可以在下面的输出部分中查看作为参考。
public String XMLtoclient() throws Exception {
String str;
JAXBContext jaxbContext = JAXBContext.newInstance(client.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
client client = (client) jaxbUnmarshaller.unmarshal(
new File("C://Users//dasso//Desktop//Migration_XML//New folder/client_configuration_CMG.xml"));
System.out.println(client);
ObjectMapper oMapper = new ObjectMapper();
@SuppressWarnings("unchecked")
Map<String, Object> map = oMapper.convertValue(client, Map.class);
System.out.println("map" + map);
map.replace("client_id", "JHKANNU", "1111");
System.out.println("Updated map" + map);
// converting map to xml
XStream magicApi = new XStream();
magicApi.registerConverter(new MapEntryConverter());
magicApi.alias("root", Map.class);
// Converting String to xml
String xml = magicApi.toXML(map);
System.out.println("Sting XML" + xml);
str = xml;
return str;
}
我在这里得到xml和Json格式的输出
@RequestMapping(value = "/xmltest", method = RequestMethod.GET, produces = { "application/json", "application/xml" })
@ResponseBody
public ResponseEntity<String> XMLtoclient() throws Exception {
System.out.println("Controller");
// I'm returning my xml here
return new ResponseEntity<String>(MigrationXmlService.XMLtoclient(), HttpStatus.OK);
}
下面是我的输入 我希望我的输出应该采用相同的格式##
作为输出,我正在获取xml,但我可以看到<ns2client_config>
的格式正确,但版本为String格式,我希望该版本也应为xml格式。##
<ns2client_config>
<client_id>JHKANNU</client_id>
<client_name>JOHN HANCOCK ANNUITIES</client_name>
<CBU>JHK</CBU>
<MRDClientMasterID>1330</MRDClientMasterID>
<version>
<id>1.0.0</id>
<validFrom>2019-02-07</validFrom>
<validTo/>
<product>
<id>DAILYEMAIL</id>
<resource_revision_id>NA</resource_revision_id>
<index_parameter>
<name>email_address</name>
<UIDisplayName>email address</UIDisplayName>
</index_parameter>
</product>
</version>
</ns2client_config>
<linked-hash-map>
<client__id>1111</client__id>
<client__name>JOHN HANCOCK ANNUITIES</client__name>
<version>[{id=0, validFrom=2019-02-07, validTo=, product=[{id=DAILYEMAIL, index_parameter=[{name=email_address, isConsumerSearchCriteria=XXXX, isConsumerDisplayValue=false, uidisplayName=first name}, {name=last_name, isConsumerSearchCriteria=false, isConsumerDisplayValue=true, isCSRSearchCriteria=false, isCSRDisplayVal]}]}]</version>
<linked-hash-map>