Home > Java > Mapping XML repeat tags to Java objects

Mapping XML repeat tags to Java objects

WBOY
Release: 2024-02-13 18:20:09
forward
710 people have browsed it

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!

source:stackoverflow.com
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template