title: Java XML XSLT to generate HTML Report
body: <p><strong>EDIT</strong> Now i know why people use string builder, using JAXB is a pain and very time consuming, at least for me it was because i have a very complex XML to generate. Appreciate the help :)</p>
  
  <p>I have a java application which collects user data.</p>
  
  <p>I also created a template XSL. Now i am stuck. What is my next step? Should i use the java to code to create an XML? If so, what kind of XML am i creating, i need my HTML to be strict at the end. Sorry if this is a duplicate, i couldn't find anything and i'm completely new to XML. So one more question is, would this be valid syntax for an xml:</p>
  
  <pre><code>StringBuilder sb = new StringBuilder(500);
  sb.append("&lt;?xml version=\"1.0\" encoding=\"UTF-8\"?&gt;\n");
  sb.append("&lt;report version=\"").append(reportVersion).append("\" generated=\"").append(scanVersion).append("\"&gt;\r\n");
  sb.append("&lt;/report &gt;");
  </code></pre>
  
  <p>The output would be</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8"?&gt;
  &lt;report version="alpha" generated="1"&gt;
  &lt;/report &gt;
  </code></pre>
  
  <p>How do i use java now to get the version and generated?</p>
  
  <p>Also i understand there is xslt 1.0 and 2.0, which one should i pick?</p>
  
  <p><strong>EDIT:</strong></p>
  
  <p>I was playing around with JAXB and got the following two classes made.</p>
  
  <pre><code>import java.util.ArrayList;
  
  import javax.xml.bind.annotation.XmlAttribute;
  import javax.xml.bind.annotation.XmlElement;
  import javax.xml.bind.annotation.XmlRootElement;
  
  @XmlRootElement
  public class Report
  {
      String              title;
      String              reportBy;
      String              reportFor;
      String              scanDate;
      String              scanVersion;
      String              reportDate;
      String              reportVersion;
      String              desc;
      ArrayList&lt;String&gt;   alertSeverity;
      ArrayList&lt;String&gt;   alertDetails;
  
      public String getTitle()
      {
          return title;
      }
  
      @XmlElement
      public void setTitle(String title)
      {
          this.title = title;
      }
  
      public String getBy()
      {
          return reportBy;
      }
  
      @XmlElement
      public void setBy(String reportBy)
      {
          this.reportBy = reportBy;
      }
  
      public String getFor()
      {
          return reportFor;
      }
  
      @XmlElement
      public void setFor(String reportFor)
      {
          this.reportFor = reportFor;
      }
  
      public String getScanDate()
      {
          return scanDate;
      }
  
      @XmlElement
      public void setScanDate(String scanDate)
      {
          this.scanDate = scanDate;
      }
  
      public String getScanVersion()
      {
          return scanVersion;
      }
  
      @XmlElement
      public void setScanVersion(String scanVersion)
      {
          this.scanVersion = scanVersion;
      }
  
      public String getReportDate()
      {
          return reportDate;
      }
  
      @XmlElement
      public void setReportDate(String reportDate)
      {
          this.reportDate = reportDate;
      }
  
      public String getReportVersion()
      {
          return reportVersion;
      }
  
      @XmlElement
      public void setReportVersion(String reportVersion)
      {
          this.reportVersion = reportVersion;
      }
  
  
      public String getDesc()
      {
          return reportVersion;
      }
  
      @XmlElement
      public void setDesc(String desc)
      {
          this.desc = desc;
      }
  }
  </code></pre>
  
  <p>and the main:</p>
  
  <pre><code>import java.io.File;
  import java.text.DateFormat;
  import java.text.SimpleDateFormat;
  import java.util.Date;
  
  import javax.xml.bind.JAXBContext;
  import javax.xml.bind.JAXBException;
  import javax.xml.bind.Marshaller;
  
  public class JAXBExample {
      public static void main(String[] args) {
          DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
          Date date = new Date();
  
          Report report = new Report();
          report.setTitle("my custom title");
          report.setBy("hans");
          report.setFor("pepe");
          report.setScanDate(dateFormat.format(date));
          report.setScanVersion("orignal version");
          report.setReportDate(dateFormat.format(date));
          report.setReportVersion("version is alpha");
          report.setDesc("some random desc");
  
        try {
  
          File file = new File("C:\\Users\\testuser\\Desktop\\file.xml");
  
  
          JAXBContext jaxbContext = JAXBContext.newInstance(Report.class);
          Marshaller jaxbMarshaller = jaxbContext.createMarshaller();
  
          // output pretty printed
          jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
  
          jaxbMarshaller.marshal(report, file);
          jaxbMarshaller.marshal(report, System.out);
  
            } catch (JAXBException e) {
          e.printStackTrace();
            }
  
      }
  }
  </code></pre>
  
  <p>I just have no idea what to do with my <code>ArrayList</code> which i need to loop to add elements in, etc.</p>
  
  <p>The current output is:</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;report&gt;
      &lt;by&gt;hans&lt;/by&gt;
      &lt;desc&gt;version is alpha&lt;/desc&gt;
      &lt;for&gt;pepe&lt;/for&gt;
      &lt;reportDate&gt;2016/02/28 18:36:33&lt;/reportDate&gt;
      &lt;reportVersion&gt;version is alpha&lt;/reportVersion&gt;
      &lt;scanDate&gt;2016/02/28 18:36:33&lt;/scanDate&gt;
      &lt;scanVersion&gt;orignal version&lt;/scanVersion&gt;
      &lt;title&gt;my custom title&lt;/title&gt;
  &lt;/report&gt;
  </code></pre>
  
  <p>Now i have two arrays that are supposed to go into this report, not sure what to do about them. Not only that but if the user did not pick the item to go in, then it's not supposed to. SO the xml won't always be the same, one attribute might not be there and sometimes it might be. Does that make sense?</p>
  
  <p><strong>EDIT 2:</strong> What i need is something like this</p>
  
  <pre><code>&lt;?xml version="1.0" encoding="UTF-8" standalone="yes"?&gt;
  &lt;report&gt;
      &lt;by&gt;hans&lt;/by&gt;
      &lt;desc&gt;version is alpha&lt;/desc&gt;
      &lt;for&gt;pepe&lt;/for&gt;
      &lt;reportDate&gt;2016/02/28 18:36:33&lt;/reportDate&gt;
      &lt;reportVersion&gt;version is alpha&lt;/reportVersion&gt;
      &lt;scanDate&gt;2016/02/28 18:36:33&lt;/scanDate&gt;
      &lt;scanVersion&gt;orignal version&lt;/scanVersion&gt;
      &lt;title&gt;my custom title&lt;/title&gt;
      &lt;site host="1" name="1" port="80" ssl="true"&gt;
          &lt;items&gt;
              &lt;item&gt;
                  &lt;name&gt;item 1&lt;/name&gt;
                  &lt;id&gt;1&lt;/id&gt;
              &lt;/item&gt;
              &lt;item&gt;
                  &lt;name&gt;item 2&lt;/name&gt;
                  &lt;id&gt;1&lt;/id&gt;
              &lt;/item&gt;
          &lt;/items&gt;
      &lt;/site&gt;
      &lt;site host="2" name="2" port="80" ssl="true"&gt;
          &lt;items&gt;
              &lt;item&gt;
                  &lt;name&gt;item 1&lt;/name&gt;
                  &lt;id&gt;1&lt;/id&gt;
              &lt;/item&gt;
              &lt;item&gt;
                  &lt;name&gt;item 2&lt;/name&gt;
                  &lt;id&gt;1&lt;/id&gt;
              &lt;/item&gt;
          &lt;/items&gt;
      &lt;/site&gt;
  &lt;/report&gt;
  </code></pre>
  
  <p>All of this is contained in an <code>ArrayList</code> How do i go about using JAXB to create this xml? Any input would be appreciated :)</p>