Saya memiliki metode seperti di bawah ini

public InstitutionsType toInstitutionPOJO(String xml) throws Exception {
        InputStream stream = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8));
        JAXBContext jaxbContext = JAXBContext.newInstance(InstitutionsType.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLEventReader someSource = factory.createXMLEventReader(stream);
        JAXBElement<InstitutionsType> userElement = jaxbUnmarshaller.unmarshal(someSource, InstitutionsType.class);
        return userElement.getValue();
    }

public ErrorType toErrorPOJO(String xml) throws Exception {
    InputStream stream = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8));
    JAXBContext jaxbContext = JAXBContext.newInstance(ErrorType.class);
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
    XMLInputFactory factory = XMLInputFactory.newInstance();
    XMLEventReader someSource = factory.createXMLEventReader(stream);
    JAXBElement<ErrorType> userElement = jaxbUnmarshaller.unmarshal(someSource, ErrorType.class);
    return userElement.getValue();
}

Saya harus membuat sekitar 14 metode serupa yang persis sama kecuali untuk tipe output. Bisakah kita menghasilkan ini?

-1
Pardha.Saradhi 22 Januari 2020, 15:54

2 jawaban

Jawaban Terbaik

Sesuatu seperti ini harus melakukan trik ..

public <T> T  toPOJO(String xml, Class<T> type) throws Exception {
        InputStream stream = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8));
        JAXBContext jaxbContext = JAXBContext.newInstance(type);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLEventReader someSource = factory.createXMLEventReader(stream);
        JAXBElement<T> userElement = jaxbUnmarshaller.unmarshal(someSource, type);
        return userElement.getValue();
}
3
Pardha.Saradhi 22 Januari 2020, 13:12

Anda dapat membuat Antarmuka yang kemudian diimplementasikan oleh kelas tipe keluaran ini. Kemudian Anda cukup mengembalikan antarmuka

public interface Outputs{
    // declare functions here
}

public class InstitutionsType implements Outputs{

    // Override the methods here   
}

Dan kemudian fungsi-fungsi ini dapat ditulis ulang sebagai:

public Outputs toInstitutionPOJO(String xml, Class<Outputs> type) throws Exception {
        InputStream stream = new ByteArrayInputStream(xml.getBytes(StandardCharsets.UTF_8));
        JAXBContext jaxbContext = JAXBContext.newInstance(InstitutionsType.class);
        Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
        XMLInputFactory factory = XMLInputFactory.newInstance();
        XMLEventReader someSource = factory.createXMLEventReader(stream);
        JAXBElement<Outputs> userElement = jaxbUnmarshaller.unmarshal(someSource, type);
        return userElement.getValue();
}
0
Rahul Agrawal 22 Januari 2020, 13:11