Table of Contents
Question content
Solution
Home Java Mapping XML repeat tags to Java objects

Mapping XML repeat tags to Java objects

Feb 13, 2024 pm 06:20 PM

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>
Copy after login

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;
Copy after login

datosrecepcion is a class with the following variables:

public class datosrecepcion implements serializable {

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

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);
Copy after login

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);
Copy after login

In cabecera class

private DatosRecepcion datos = new DatosRecepcion();
Copy after login

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 of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

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

Roblox: Bubble Gum Simulator Infinity - How To Get And Use Royal Keys
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Nordhold: Fusion System, Explained
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌
Mandragora: Whispers Of The Witch Tree - How To Unlock The Grappling Hook
3 weeks ago By 尊渡假赌尊渡假赌尊渡假赌

Hot Tools

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

SublimeText3 Chinese version

SublimeText3 Chinese version

Chinese version, very easy to use

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

Dreamweaver CS6

Dreamweaver CS6

Visual web development tools

SublimeText3 Mac version

SublimeText3 Mac version

God-level code editing software (SublimeText3)

Hot Topics

Java Tutorial
1666
14
PHP Tutorial
1273
29
C# Tutorial
1253
24