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!
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
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!