EDIT 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 :)
I have a java application which collects user data.
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:
StringBuilder sb = new StringBuilder(500);
sb.append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");
sb.append("<report version=\"").append(reportVersion).append("\" generated=\"").append(scanVersion).append("\">\r\n");
sb.append("</report >");
The output would be
<?xml version="1.0" encoding="UTF-8"?>
<report version="alpha" generated="1">
</report >
How do i use java now to get the version and generated?
Also i understand there is xslt 1.0 and 2.0, which one should i pick?
EDIT:
I was playing around with JAXB and got the following two classes made.
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<String> alertSeverity;
ArrayList<String> 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;
}
}
and the main:
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();
}
}
}
I just have no idea what to do with my ArrayList which i need to loop to add elements in, etc.
The current output is:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<report>
<by>hans</by>
<desc>version is alpha</desc>
<for>pepe</for>
<reportDate>2016/02/28 18:36:33</reportDate>
<reportVersion>version is alpha</reportVersion>
<scanDate>2016/02/28 18:36:33</scanDate>
<scanVersion>orignal version</scanVersion>
<title>my custom title</title>
</report>
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?
EDIT 2: What i need is something like this
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<report>
<by>hans</by>
<desc>version is alpha</desc>
<for>pepe</for>
<reportDate>2016/02/28 18:36:33</reportDate>
<reportVersion>version is alpha</reportVersion>
<scanDate>2016/02/28 18:36:33</scanDate>
<scanVersion>orignal version</scanVersion>
<title>my custom title</title>
<site host="1" name="1" port="80" ssl="true">
<items>
<item>
<name>item 1</name>
<id>1</id>
</item>
<item>
<name>item 2</name>
<id>1</id>
</item>
</items>
</site>
<site host="2" name="2" port="80" ssl="true">
<items>
<item>
<name>item 1</name>
<id>1</id>
</item>
<item>
<name>item 2</name>
<id>1</id>
</item>
</items>
</site>
</report>
All of this is contained in an ArrayList How do i go about using JAXB to create this xml? Any input would be appreciated :)
ArrayListI wasn't sure what to do. I'll update with where i had left off.Reports or marshal everyReportseperately (or marshal fragments).