1. XML

<?xml version="1.0" encoding="euc-kr"?>
<items>
    <item>
        <name>Seoul</name>
        <code>K1</code>
    </item>
    <item>
        <name>Seattle</name>
        <code>U3/</code>
    </item>
    <item>
        <name>Osaka</name>
        <code>J2</code>
    </item>
</items>

 

2. Item.java

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(code = "item")
@XmlAccessorType (XmlAccessType.FIELD)
public class Item {
    String name;
    String code;
    
    public String getName() {
        return name
    }
    public void setName(String name{
        this.name = name
    }
    public String getCode() {
        return code
    }
    public void setCode(String code) {
        this.code = code;
    }
}

 

3. Items.java

import java.util.List;

import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "items")
@XmlAccessorType(XmlAccessType.FIELD)
public class items {
	@XmlElement(name = "item")
	private List<Item> items = null;

	public List<Item> getitems() {
		return items;
	}

	public void setitems(List<Item> items) {
		this.items = items;
	}
}

 

4. Logic

JAXBContext jaxbContext;

try {
	jaxbContext = JAXBContext.newInstance(Items.class);
	Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();

	File file = new File("my.xml");
	Items Items = (Items)unmarshaller.unmarshal(file);
	
	for(Item Item : Items.getItems()) {
                System.out.println(Item.getName());
                System.out.println(Item.getCode());
    }
} catch (JAXBException e) {
	e.printStackTrace();
}