Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.io.StringReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlRootElement;
import javax.xml.bind.annotation.XmlValue;

@XmlRootElement(name = "element")
public class Main {

    protected String elementValue;

    @XmlValue
    public String getElementValue() {
        return elementValue;
    }

    public void setElementValue(String el) {
        this.elementValue = el;
    }

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Main.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        StringReader xml = new StringReader("<element>data</element>");
        Main myClass = (Main) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(myClass, System.out);
    }

}