Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.io.File;
import java.util.List;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import javax.xml.bind.Unmarshaller;
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.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;

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

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("input.xml");
        WebResponse wr = (WebResponse) unmarshaller.unmarshal(xml);

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

    @XmlRootElement(name = "WebResponse")
    @XmlAccessorType(XmlAccessType.FIELD)
    public static class WebResponse {
        @XmlElement(name = "Response")
        private Response response = null;
    }

    @XmlAccessorType(XmlAccessType.FIELD)
    public static class Response {
        @XmlElementWrapper(name = "Info")
        @XmlElement(name = "Person")
        private List<Person> person;
        @XmlAttribute
        private String target;
    }

    @XmlAccessorType(XmlAccessType.FIELD)
    public static class Person {
        @XmlElement(name = "Id")
        private int id;
    }
}