Use JAXB to create Object from XML String

前端 未结 5 818
一向
一向 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:39

    To pass XML content, you need to wrap the content in a Reader, and unmarshal that instead:

    JAXBContext jaxbContext = JAXBContext.newInstance(Person.class);
    Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
    
    StringReader reader = new StringReader("xml string here");
    Person person = (Person) unmarshaller.unmarshal(reader);
    

提交回复
热议问题