Showing posts with label xml. Show all posts
Showing posts with label xml. Show all posts

Thursday, January 14, 2016

JAXB: Marshalling entity to String example

 Here is an example / template for marshalling entity to String:

 Marshalling code fragment

java.io.StringWriter sw = new StringWriter();

JAXBContext pContext = JAXBContext.newInstance(SomeEntity.class);

Marshaller marshaller = pContext.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_ENCODING, "UTF-8");
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(dto, sw);

sw.toString();

SomeEntity.java

@XmlRootElement(name = "result")
@XmlAccessorType(XmlAccessType.FIELD)
public class SomeEntity{
    private String someVar;
    private String someOtherVal;
    ...
    // getters & setters
}

Tuesday, December 29, 2015

JAXB: Marshalling kickstart blank template application

This is a simple JAXB application, that could be used as template. It has console (and file) output.

import java.io.File;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlAttribute;

@XmlRootElement
public class Test {
    String name;
    int age;
    int id;

    public String getName() {
        return name;
    }

    @XmlElement
    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    @XmlElement
    public void setAge(int age) {
        this.age = age;
    }

    public int getId() {
        return id;
    }

    @XmlAttribute
    public void setId(int id) {
        this.id = id;
    }



    public static void main(String[] args) {

        Test test = new Test();
        test.setId(100);
        test.setName("Tim Rott");
        test.setAge(29);

        try {

//            File file = new File("C:\\file.xml");
            JAXBContext jaxbContext = JAXBContext.newInstance(Test.class);
            Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

            // output pretty printed
            jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

//            jaxbMarshaller.marshal(test, file);
            jaxbMarshaller.marshal(test, System.out);

        } catch (JAXBException e) {
            e.printStackTrace();
        }

    }
}

JAXB: Generate schema and generate Java class

You could easily generate .xsd file from java model class and vice versa.

Generate the schema

Lets have a model class as follows:

@XmlRootElement(name="product")
@XmlAccessorType(XmlAccessType.FIELD)
public class Product {

    @XmlElement(required=true) 
    protected int id;
    @XmlElement(required=true) 
    protected String name;
    @XmlElement(required=true) 
    protected String description;
    @XmlElement(required=true) 
    protected int price;
    
    public Product() {}
    
    // Getter and setter methods
    // ...
}
 
Run the JAXB schema generator on the command line to generate the corresponding XML schema definition:

schemagen Product.java

This command produces the XML schema as an .xsd file.

Generate Java class

Lets have schema definition in .xsd file:

<?xml version="1.0"?>
<xs:schema targetNamespace="http://xml.product" 
           xmlns:xs="http://www.w3.org/2001/XMLSchema"    
           elementFormDefault="qualified"
           xmlns:myco="http://xml.product">

  <xs:element name="product" type="myco:Product"/>

  <xs:complexType name="Product">
    <xs:sequence>
      <xs:element name="id" type="xs:int"/>
      <xs:element name="name" type="xs:string"/>
      <xs:element name="description" type="xs:string"/>
      <xs:element name="price" type="xs:int"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>
 
Run the schema compiler tool on the command line as follows:

xjc Product.xsd
 
This will produce the following class:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Product", propOrder = {
    "id",
    "name",
    "description",
    "price"
})
public class Product {
    protected int id;
    @XmlElement(required = true)
    protected String name;
    @XmlElement(required = true)
    protected String description;
    protected int price;

    // Setter and getter methods
    // ...
}

Source

Source: https://docs.oracle.com/javaee/6/tutorial/doc/gkknj.html