如何使用jaxb将xml字符串解组到java对象
我需要使用rest模板发送请求。在发送请求之前,我需要在发送请求时将对象整理成xml。我收到了请求的响应,但是是XML格式的。然后,我需要将响应xml转换为对象,以便在接口上显示结果 下面是我发送请求的控制器如何使用jaxb将xml字符串解组到java对象,java,xml,jaxb,unmarshalling,Java,Xml,Jaxb,Unmarshalling,我需要使用rest模板发送请求。在发送请求之前,我需要在发送请求时将对象整理成xml。我收到了请求的响应,但是是XML格式的。然后,我需要将响应xml转换为对象,以便在接口上显示结果 下面是我发送请求的控制器 @RequestMapping("/searchSummon") public String Search(Model model) { model.addAttribute("jaxbExample", new JAXBExample()); model.addA
@RequestMapping("/searchSummon")
public String Search(Model model)
{
model.addAttribute("jaxbExample", new JAXBExample());
model.addAttribute("pdxiRes", new PDXIRes());
JAXBExample jaxbExample = new JAXBExample();
String create_xml = jaxbExample.CreateXML();
System.out.println(create_xml);
RestTemplate restTemplate = new RestTemplate();
String a = restTemplate.postForObject("http://192.168.80.30/summon-
V2/example", "<?xml version='1.0' encoding='UTF-8'?> <!DOCTYPE PDXIReq
SYSTEM 'summon.dtd'>" + create_xml,String.class);
System.out.println(a);
return "searchSummon";
}
@RequestMapping(“/searchcall”)
公共字符串搜索(模型)
{
addAttribute(“jaxbExample”,新的jaxbExample());
addAttribute(“pdxiRes”,新的pdxiRes());
JAXBExample JAXBExample=新的JAXBExample();
字符串create_xml=jaxbExample.CreateXML();
System.out.println(创建xml);
RestTemplate RestTemplate=新RestTemplate();
字符串a=restTemplate.postForObject(“http://192.168.80.30/summon-
V2/示例“,”+创建xml,String.class);
系统输出打印项次(a);
返回“搜索召唤”;
}
如何将“a”解组到对象?
响应类
PDXIRes
标题
要求
细节
状态
响应('a')的xml
abc017637m
683642435
俏皮话
1024
2.
1810000200002AQ639332
NN162
181000020402AM947772
NN162您可以使用解组器来实现这一点
JAXBContext jaxbContext = JAXBContext.newInstance(Person.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader("xml string here");
Person person = (Person) unmarshaller.unmarshal(reader);
在此处找到:如果您要使用像Jersey这样的REST框架,编组/解编过程将自动处理…请检查是否有帮助。。。
JAXBContext jaxbContext = JAXBContext.newInstance(Person.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
StringReader reader = new StringReader("xml string here");
Person person = (Person) unmarshaller.unmarshal(reader);