How to create an XML Log file in Java using JAXB (Customized)

Using JAX-B, its amazing easy to convert java objects to xml and vice versa.
Java Object  --> XML (Marshalling)
XML --> Java Object (Unmarshalling)

We are going to create 3 DTO's and then, using JAX-B, will print the output to a log file.
The DTO's have following hierarchy:
TestSteps :--> TestCase :--> TestCases.

The Main Class:


/**
 *
 */
package main;

import logger.Logger;

/**
 * @author sundaram
 *
 */
public class Main {

/**
* @param args
*/
public static void main(String[] args) {

Logger logger = new Logger("SampleLog1");

logger.writeToLogTestCase(1.0, "Title 1");
logger.writeToLogTestStep(1.0, 1.1, "Verify that part 1", "PASS",
"Function FAM_ClientNotesPane has been executed successfully.",
"[Table:NotesPanes; 1] Action = Action,ExecuteVerify = ExecuteVerify");
          }
}


The Logger Class:

Will Upload it shortly.

Here are the 3 DTO's:

(1) Class Name: TestStep:


//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a>
//


package dto;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlValue;


/**
 * @author sundaram
 *
 * <p>Java class for testStepType complex type.
 *
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class TestStep {

@XmlElement
    protected double testStepId;
@XmlElement
    protected String testStepDescription;
@XmlElement
    protected String testStepResult;
@XmlElement
    protected String resultDescription;
@XmlElement
    protected String release;
@XmlElement
    protected String startTime;
@XmlElement
    protected String execTime;
@XmlElement
    protected String call;
@XmlElement
    protected String data;
@XmlElement
    protected String trace;
@XmlElement
    protected String link;
@XmlElement
    protected String cpuUsage;
@XmlElement
    protected String memoryUsage;
 
/**
* @return the testStepId
*/
public double getTestStepId() {
return testStepId;
}
/**
* @param testStepId the testStepId to set
*/
public void setTestStepId(double testStepId) {
this.testStepId = testStepId;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "TestStepType [testStepId=" + testStepId
+ ", testStepDescription=" + testStepDescription
+ ", testStepResult=" + testStepResult + ", resultDescription="
+ resultDescription + ", release=" + release + ", startTime="
+ startTime + ", execTime=" + execTime + ", call=" + call
+ ", data=" + data + ", trace=" + trace + ", link=" + link
+ ", cpuUsage=" + cpuUsage + ", memoryUsage=" + memoryUsage
+ "]";
}
/**
* @return the tesetStepDescription
*/
public String getTestStepDescription() {
return testStepDescription;
}
/**
* @param tesetStepDescription the tesetStepDescription to set
*/
public void setTestStepDescription(String testStepDescription) {
this.testStepDescription = testStepDescription;
}
/**
* @return the testStepResult
*/
public String getTestStepResult() {
return testStepResult;
}
/**
* @param testStepResult the testStepResult to set
*/
public void setTestStepResult(String testStepResult) {
this.testStepResult = testStepResult;
}
/**
* @return the resultDescription
*/
public String getResultDescription() {
return resultDescription;
}
/**
* @param resultDescription the resultDescription to set
*/
public void setResultDescription(String resultDescription) {
this.resultDescription = resultDescription;
}
/**
* @return the release
*/
public String getRelease() {
return release;
}
/**
* @param release the release to set
*/
public void setRelease(String release) {
this.release = release;
}
/**
* @return the startTime
*/
public String getStartTime() {
return startTime;
}
/**
* @param startTime the startTime to set
*/
public void setStartTime(String startTime) {
this.startTime = startTime;
}
/**
* @return the execTime
*/
public String getExecTime() {
return execTime;
}
/**
* @param execTime the execTime to set
*/
public void setExecTime(String execTime) {
this.execTime = execTime;
}
/**
* @return the call
*/
public String getCall() {
return call;
}
/**
* @param call the call to set
*/
public void setCall(String call) {
this.call = call;
}
/**
* @return the data
*/
public String getData() {
return data;
}
/**
* @param data the data to set
*/
public void setData(String data) {
this.data = data;
}
/**
* @return the trace
*/
public String getTrace() {
return trace;
}
/**
* @param trace the trace to set
*/
public void setTrace(String trace) {
this.trace = trace;
}
/**
* @return the link
*/
public String getLink() {
return link;
}
/**
* @param link the link to set
*/
public void setLink(String link) {
this.link = link;
}
/**
* @return the cpuUsage
*/
public String getCpuUsage() {
return cpuUsage;
}
/**
* @param cpuUsage the cpuUsage to set
*/
public void setCpuUsage(String cpuUsage) {
this.cpuUsage = cpuUsage;
}
/**
* @return the memoryUsage
*/
public String getMemoryUsage() {
return memoryUsage;
}
/**
* @param memoryUsage the memoryUsage to set
*/
public void setMemoryUsage(String memoryUsage) {
this.memoryUsage = memoryUsage;
}
}

(2) Class Name: TestCase:-

//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
//


package dto;

import java.util.List;
import java.util.Set;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlType;
import javax.xml.bind.annotation.XmlValue;


/**
 * @author M1018094
 * 
 * <p>Java class for testCaseType complex type.  
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class TestCase {
@XmlElement
    protected double testCaseNumber;
@XmlElement
    protected String testCaseTitle;
@XmlElement
    protected String startTime;
@XmlElement
    protected List<TestStep> TestStep;
public double getTestCaseNumber() {
return testCaseNumber;
}
public void setTestCaseNumber(double testCaseNumber) {
this.testCaseNumber = testCaseNumber;
}
public String getTestCaseTitle() {
return testCaseTitle;
}
public void setTestCaseTitle(String testCaseTitle) {
this.testCaseTitle = testCaseTitle;
}
public String getStartTime() {
return startTime;
}
public void setStartTime(String startTime) {
this.startTime = startTime;
}
public List<TestStep> getTestStep() {
return TestStep;
}
public void setTestStep(List<TestStep> testStep) {
TestStep = testStep;
}
@Override
public String toString() {
return "TestCase [testCaseNumber=" + testCaseNumber
+ ", testCaseTitle=" + testCaseTitle + ", startTime="
+ startTime + ", TestStep=" + TestStep + "]";
}
  
}


(3) Class Name: TestCases:-

//
// This file was generated by the JavaTM Architecture for XML Binding(JAXB) Reference Implementation, vJAXB 2.1.10 in JDK 6 
// See <a href="http://java.sun.com/xml/jaxb">http://java.sun.com/xml/jaxb</a> 
//

package dto;

import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;


/**
 * @author M1018094
 * 
 * <p>Java class for testCasesType complex type.  
 * 
 */
@XmlAccessorType(XmlAccessType.FIELD)
@XmlRootElement
public class TestCases {
@XmlElement
    protected List<TestCase> TestCase;

public List<TestCase> getTestCase() {
return TestCase;
}

public void setTestCase(List<TestCase> testCase) {
TestCase = testCase;
}

@Override
public String toString() {
return "TestCasesType [TestCases=" + TestCase + "]";
}

}






Comments

Popular posts from this blog

JMeter – Working with JSON

Existence: Absolute & Relative