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.Unmarshaller;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

public class Main {

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

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        StringReader xml = new StringReader(
                "<person><id>123</id><first-name>Tom</first-name><last-name>Smith</last-name></person>");
        Person person = (Person) unmarshaller.unmarshal(xml);

        System.out.println(person.id);
        System.out.println(person.firstName);
        System.out.println(person.lastName);
    }

    @XmlRootElement
    @XmlAccessorType(XmlAccessType.FIELD)
    static class Person {
        String id;

        @XmlElement(name = "first-name")
        String firstName;

        @XmlElement(name = "last-name")
        String lastName;
    }

}