Map XML repet tag to Java object

I have the next xml:

<mensajeWS>
    <respuestaGetCabecerasType>
        <cabeceras>
            <cabecera>
                <id>1234</id>
                <tipoMensaje>3</tipoMensaje>
                <datos>
                    <fechaEnvio>2023-12-13T00:05:00</fechaEnvio>
                    <fechaEnvio>2023-12-14T00:05:00</fechaEnvio>
                </datos>
            </cabecera>
        </cabeceras>
    </respuestaGetCabecerasType>
</mensajeWS>

And I want to map their data into the following object:

public class Cabecera implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -865317642824095952L;
    
    private int id;
    
    private int tipoMensaje;
    
    private DatosRecepcion datos;

DatosRecepcion is a class with the following variable:

public class DatosRecepcion implements Serializable {

    /**
     * 
     */
    private static final long serialVersionUID = -6703565058845907875L;
    
    private List<String> fechaEnvio;


To map the xml data to the object, do the following:

List<Cabecera> listaCabeceras = new ArrayList<Cabecera>();
            DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
            DocumentBuilder builder = factory.newDocumentBuilder();
            Document document = builder.parse(new InputSource(new StringReader(xml.toString())));
            
            System.out.println("Root element: " + document.getDocumentElement().getNodeName());
            
            NodeList nList = document.getElementsByTagName("cabecera");
            for(int i = 0; i < nList.getLength(); i++) {
                Node nNode = nList.item(i);
                System.out.println("Current Element: " + nNode.getNodeName());
                
                Element element = (Element) nNode;
                System.out.println("id: " + element.getElementsByTagName("id").item(0).getTextContent());
                System.out.println("Tipo de mensaje: " + element.getElementsByTagName("tipoMensaje").item(0).getTextContent());
                System.out.println("Fecha de Envio: " + element.getElementsByTagName("fechaEnvio").item(0).getTextContent());
                
                Cabecera cabecera = new Cabecera();
                cabecera.setId(Integer.parseInt(element.getElementsByTagName("id").item(0).getTextContent()));
                cabecera.setTipoMensaje(Integer.parseInt(element.getElementsByTagName("tipoMensaje").item(0).getTextContent()));
                //cabecera.getDatos().setFechaEnvio(element.getElementsByTagName("fechaEnvio").item(0).getTextContent());
                listaCabeceras.add(cabecera);

I would like to know how to map a repeating tag like fechaEnvio to my Java Object. I use version 8 of Java and they won’t let me use a higher one at the moment

Thanks.

Map XML repet tag to Java object

  • Have you considered using jackson for xml serialization?

    – 

  • Is there a reason you can’t write element.getElementsByTagName("fechaEnvio")?

    – 

  • What can i to write?

    – 

Leave a Comment