title: Java: How could we convert an ArrayList to XML?
body: <p>Hello and thank you for reading this question:</p>
  
  <p>I will explain the use case first and the approach I have tried:</p>
  
  <p>I have an ArrayList which holds Courses'data and it is being created in the FrontServlet and it is being stored in the session.</p>
  
  <p>I would like to convert the ArrayList to XML to show it as HTML transforming it with XSL.</p>
  
  <p>Mi doubt is: how could we convert an ArrayList to XML?</p>
  
  <p>I have studied how to annotate classes to transform them: <a href="https://www.mkyong.com/java/jaxb-hello-world-example/" rel="nofollow noreferrer">https://www.mkyong.com/java/jaxb-hello-world-example/</a></p>
  
  <p>I have used an static XML to convert it and show it: <a href="https://stackoverflow.com/questions/48870107/java-implementing-transform-view-pattern-to-convert-xml-to-html-with-an-xsl-fi">Java: Implementing Transform View pattern, to convert XML to HTML with an XSL file</a></p>
  
  <p>I have read a good post about extending ArrayList&lt;> to be able to transform it: <a href="https://stackoverflow.com/questions/4152269/why-my-arraylist-is-not-marshalled-with-jaxb">Why my ArrayList is not marshalled with JAXB?</a></p>
  
  <p>I first tried with:</p>
  
  <pre><code>/*
   * To change this license header, choose License Headers in Project Properties.
   * To change this template file, choose Tools | Templates
   * and open the template in the editor.
   */
  package org;
  
  import javax.xml.bind.annotation.XmlElement;
  import javax.xml.bind.annotation.XmlRootElement;
  
  /**
   *
   * @author YonePC
   */
  @XmlRootElement
  public class Curso {
  
      @XmlElement
      public void setTitulo(String titulo) {
          this.titulo = titulo;
      }
  
      @XmlElement
      public void setAutor(String autor) {
          this.autor = autor;
      }
  
      @XmlElement
      public void setAsignatura(String asignatura) {
          this.asignatura = asignatura;
      }
  
      @XmlElement
      public void setDuracion(String duracion) {
          this.duracion = duracion;
      }
  
      @XmlElement
      public void setVideo(String video) {
          this.video = video;
      }
      String titulo, autor, asignatura, duracion, video;
  
      public Curso(String titulo, String autor, String asignatura, String duracion, String video) {
          this.titulo = titulo;
          this.autor = autor;
          this.asignatura = asignatura;
          this.duracion = duracion;
          this.video = video;
      }
  
      public String getTitulo() {
          return titulo;
      }
  
      public String getAutor() {
          return autor;
      }
  
      public String getAsignatura() {
          return asignatura;
      }
  
      public String getDuracion() {
          return duracion;
      }
  
      public String getVideo() {
          return video;
      }
  
  }
  </code></pre>
  
  <p>But I do not need the Curso class itself, I need to convert the ArrayList which stores them.</p>
  
  <p>In addition, I currently stores the courses being created in the session. How could we get the session's info in a servlet? I mean, we can access them from the servlet where it has being currently created, and from JSP pages, but how do we do access into servlets?</p>
  
  <p>I ask the previous question because in all the examples I found about converting Java to XML, they showed new instances and not how to convert previous stored ones.</p>
  
  <p>The code I have tried:</p>
  
  <pre><code>/*
   * To change this license header, choose License Headers in Project Properties.
   * To change this template file, choose Tools | Templates
   * and open the template in the editor.
   */
  package frontController;
  
  import java.io.File;
  import java.io.IOException;
  import java.io.PrintWriter;
  import java.util.ArrayList;
  import java.util.Iterator;
  import java.util.logging.Level;
  import java.util.logging.Logger;
  import javax.servlet.ServletException;
  import javax.servlet.annotation.WebServlet;
  import javax.servlet.http.HttpServlet;
  import javax.servlet.http.HttpServletRequest;
  import javax.servlet.http.HttpServletResponse;
  import javax.servlet.http.HttpSession;
  import javax.xml.bind.JAXBContext;
  import javax.xml.bind.JAXBException;
  import javax.xml.bind.Marshaller;
  import javax.xml.transform.Result;
  import javax.xml.transform.Transformer;
  import javax.xml.transform.TransformerConfigurationException;
  import javax.xml.transform.TransformerException;
  import javax.xml.transform.TransformerFactory;
  import javax.xml.transform.stream.StreamResult;
  import javax.xml.transform.stream.StreamSource;
  import org.Curso;
  
  /**
   *
   * @author YonePC
   */
  @WebServlet(name = "CourseInfoCommand", urlPatterns = {"/CourseInfoCommand"})
  public class CourseInfoCommand extends FrontCommand {
  
      /**
       * Processes requests for both HTTP &lt;code&gt;GET&lt;/code&gt; and &lt;code&gt;POST&lt;/code&gt;
       * methods.
       *
       * @param request servlet request
       * @param response servlet response
       * @throws ServletException if a servlet-specific error occurs
       * @throws IOException if an I/O error occurs
       */
      protected void processRequest(HttpServletRequest request, HttpServletResponse response)
              throws ServletException, IOException {
          response.setContentType("text/html;charset=UTF-8");
  
      }
  
      // &lt;editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code."&gt;
      /**
       * Handles the HTTP &lt;code&gt;GET&lt;/code&gt; method.
       *
       * @param request servlet request
       * @param response servlet response
       * @throws ServletException if a servlet-specific error occurs
       * @throws IOException if an I/O error occurs
       */
      protected void doGet(HttpServletRequest request, HttpServletResponse response)
              throws ServletException, IOException {
          processRequest(request, response);
      }
  
      /**
       * Handles the HTTP &lt;code&gt;POST&lt;/code&gt; method.
       *
       * @param request servlet request
       * @param response servlet response
       * @throws ServletException if a servlet-specific error occurs
       * @throws IOException if an I/O error occurs
       */
      protected void doPost(HttpServletRequest request, HttpServletResponse response)
              throws ServletException, IOException {
          processRequest(request, response);
      }
  
      /**
       * Returns a short description of the servlet.
       *
       * @return a String containing servlet description
       */
      public String getServletInfo() {
          return "Short description";
      }// &lt;/editor-fold&gt;
  
      @Override
      public void process(HttpServletRequest request) {
          try {
  
              File file = new File("C:\\Users\\YonePC\\Videos\\ASAPLICACIONCURSOSPRACTICA1\\src\\java\\frontController\\Cursos.xml");
              ArrayList courses = (ArrayList) session.getAttribute("cursos");
  
              JAXBContext jaxbContext = JAXBContext.newInstance(ArrayListCourses.class);
              Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
  
              jaxbMarshaller.marshal(courses, file);
  
              TransformerFactory factory = TransformerFactory.newInstance();
              StreamSource xsl = new StreamSource(new File(""));
              Transformer newTransformer = factory.newTransformer(xsl);
  
              StreamSource xml = new StreamSource(new File(""));
              PrintWriter writer = response.getWriter();
              Result result = new StreamResult(writer);
              newTransformer.transform(xml, result);
              writer.println(writer.toString());
  
              forward("/CourseInfo.jsp");
          } catch (ServletException ex) {
              Logger.getLogger(CourseInfoCommand.class.getName()).log(Level.SEVERE, null, ex);
          } catch (IOException ex) {
              Logger.getLogger(CourseInfoCommand.class.getName()).log(Level.SEVERE, null, ex);
          } catch (TransformerConfigurationException ex) {
              Logger.getLogger(CourseInfoCommand.class.getName()).log(Level.SEVERE, null, ex);
          } catch (TransformerException ex) {
              Logger.getLogger(CourseInfoCommand.class.getName()).log(Level.SEVERE, null, ex);
          } catch (JAXBException ex) {
              Logger.getLogger(CourseInfoCommand.class.getName()).log(Level.SEVERE, null, ex);
          }
      }
  
  }
  </code></pre>
  
  <p>Thank you for your help.</p>