Use JAXB to create Object from XML String

前端 未结 5 816
一向
一向 2020-12-02 04:05

How can I use the below code to unmarshal a XML string an map it to the JAXB object below?

JAXBContext jaxbContext = JAXBContext.newInstance(Person.class);
U         


        
5条回答
  •  爱一瞬间的悲伤
    2020-12-02 04:49

    If you want to parse using InputStreams
    
    public Object xmlToObject(String xmlDataString) {
            Object converted = null;
            try {
            
                JAXBContext jc = JAXBContext.newInstance(Response.class);
            
                Unmarshaller unmarshaller = jc.createUnmarshaller();
                InputStream stream = new ByteArrayInputStream(xmlDataString.getBytes(StandardCharsets.UTF_8));
                
                converted = unmarshaller.unmarshal(stream);
            } catch (JAXBException e) {
                e.printStackTrace();
            }
            return converted;
        }
    

提交回复
热议问题