How to Serialize List?

后端 未结 7 671
长发绾君心
长发绾君心 2020-11-30 11:05

I have Class A. Class B and Class C are part of Class A.

Class A 
{

//Few Properties of Class A

List list1 = new List()

         


        
7条回答
  •  有刺的猬
    2020-11-30 12:10

    JAXB is the best I ever used.

    http://www.oracle.com/technetwork/articles/javase/index-140168.html

    From XML:

    QuestionEntity obj = null;
    try {
        JAXBContext ctx = JAXBContext.newInstance(QuestionEntity.class);
        Unmarshaller unmarshaller = ctx.createUnmarshaller();
        obj = (QuestionEntity) unmarshaller.unmarshal(new StreamSource(new StringReader(xml)));
    } catch (JAXBException e) {
        e.printStackTrace();
    }
    

    To XML:

    JAXBContext ctx = JAXBContext.newInstance(TestEntity.class);
    Marshaller marshaller = ctx.createMarshaller();
    marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
    StringWriter stringWriter = new StringWriter();
    marshaller.marshal(this, stringWriter);
    return stringWriter.toString();
    

提交回复
热议问题