search
HomeJavaMapping XML repeat tags to Java objects

php editor Apple will introduce you to how to map XML repeat tags to Java objects in this article. XML is a commonly used data exchange format, and Java objects are data structures we often use in programming. Mapping XML repeat tags to Java objects can help us process and manipulate data more conveniently. This article will break down the process for you in detail and provide practical examples to help you understand better. Let’s explore this fun and practical topic together!

Question content

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>

I want to map their data to the following objects:

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 variables:

public class datosrecepcion implements serializable {

    /**
     * 
     */
    private static final long serialversionuid = -6703565058845907875l;
    
    private list<string> fechaenvio;

To map xml data to objects, 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 want to know how to map repeating tags like fechaenvio to my java object. I'm using java 8 version and they currently won't let me use higher versions

Thanks.

Map xml repeat tags to java objects

Solution

You can do this:

node datosnode = element.getelementsbytagname("datos").item(0);
element elementdatos = (element) datosnode;
nodelist fechaenviolist = elementdatos.getelementsbytagname("fechaenvio");
list<string> data = new arraylist<>();
for (i = 0; i < fechaenviolist.getlength(); i++) {
    data.add(fechaenviolist.item(i).gettextcontent());
}

cabecera.getdatos().setfechaenvio(data);
listacabeceras.add(cabecera);

In cabecera class

private DatosRecepcion datos = new DatosRecepcion();

The above is the detailed content of Mapping XML repeat tags to Java objects. For more information, please follow other related articles on the PHP Chinese website!

Statement
This article is reproduced at:stackoverflow. If there is any infringement, please contact admin@php.cn delete

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Atom editor mac version download

Atom editor mac version download

The most popular open source editor

WebStorm Mac version

WebStorm Mac version

Useful JavaScript development tools

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft