Main.java Source code

Java tutorial

Introduction

Here is the source code for Main.java

Source

import java.io.File;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;

public class Main {

    public static void main(String[] args) throws Exception {
        Map map = new HashMap<String, String>();
        map.put("cluster", "10.200.111.111");
        map.put("cluster1", "10.200.121.111");

        Product xml = new Product();
        List<Top> top1 = new ArrayList<Top>();
        Set<String> keys = map.keySet();
        for (String key : keys) {
            Top top = new Top();
            top.setMode(key);
            top.setAddress((String) map.get(key));
            top1.add(top);
        }
        xml.setTop(top1);
        File file = new File("C:\\kar\\file.xml");
        JAXBContext jaxbContext = JAXBContext.newInstance(Product.class);
        Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

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

        jaxbMarshaller.marshal(xml, file);
        jaxbMarshaller.marshal(xml, System.out);
    }
}

@XmlRootElement(name = "top")
@XmlType(name = "top")
@XmlAccessorType(XmlAccessType.FIELD)
public class Top {

    @XmlElement(name = "mode", required = true)
    private String mode;

    @XmlElement(name = "mode", required = true)
    private String address;

    public String getMode() {
        return mode;
    }

    public void setMode(String mode) {
        this.mode = mode;
    }

    public String getAddress() {
        return address;
    }

    public void setAddress(String address) {
        this.address = address;
    }
}

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

    @XmlElement(name = "Top")
    private List<Top> top;

    public List<Top> getTop() {
        return top;
    }

    public void setTop(List<Top> top) {
        this.top = top;
    }

}