Bu örnekte bir XML'den Java nesnesini yüklüyoruz. Bunun için Java Architecture for XML Binding (JAXB) kütüphanesini kullanıyoruz. XML'imiz aşağıdaki gibi olsun :
<commands>
<command key="EN">
<p name="Go" required="true" type="integer"/>
<p name="Come" required="true" type="string"/>
</command>
<command key="TR">
<p name="Gel" required="true" type="string"/>
<p name="Git" required="true" type="float"/>
</command>
</commands>
Görüldüğü gibi commands kök (root) elementidir. İçerisinde iki tane komut vardır. Komutlarında parametreleri vardır ve p etiketi ile verilmiştir. Java sınıfları aşağıdaki gibidir.
Paramtre sınıfı :
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlAttribute;
import org.apache.commons.lang3.builder.ToStringBuilder;
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Parameter {
private String name;
private String type;
private boolean required;
public Parameter() {}
public Parameter(String name, String type, boolean required) {
super();
this.name = name;
this.type = type;
this.required = required;
}
@XmlAttribute
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlAttribute
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
@XmlAttribute(name = "required")
public boolean isRequired() {
return required;
}
public void setRequired(boolean required) {
this.required = required;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
Komut için sınıf :
import java.util.List;
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 org.apache.commons.lang3.builder.ToStringBuilder;
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Command {
private String key;
private List<Parameter> parameters;
public Command() {
}
public Command(String key, List<Parameter> parameters) {
super();
this.key = key;
this.parameters = parameters;
}
@XmlAttribute
public String getKey() {
return key;
}
public void setKey(String key) {
this.key = key;
}
@XmlElement(name = "p")
public List<Parameter> getParameters() {
return parameters;
}
public void setParameters(List<Parameter> parameters) {
this.parameters = parameters;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
En son da ana sınıfı tanımlayalım :
import java.io.File;
import java.util.Collections;
import java.util.List;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
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;
import org.apache.commons.lang3.builder.ToStringBuilder;
@XmlRootElement (name = "commands")
@XmlAccessorType(XmlAccessType.PROPERTY)
public class Commands {
private List<Command> commands;
public Commands() {
}
public Commands(List<Command> commands) {
super();
this.commands = commands;
}
@XmlElement(name = "command")
public List<Command> getCommands() {
return commands;
}
public void setCommands(List<Command> commands) {
this.commands = commands;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
}
Bu sınıflar yaratıldığı için artık XML'den sınıfı yüklüyebiliriz :
public static void main(String[] args)
throws JAXBException {
JAXBContext jaxbContext = JAXBContext.newInstance(Commands.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
Commands commands= (Commands) jaxbUnmarshaller.unmarshal(
new File("D:\\commands.xml"));
System.out.println(commands);
}
Burada bir klasör içindeki XML yüklenmektedir. Commands'ı ekrana bastırdığınızda XML'den bilgilerin yüklendiğini ekranda göreceksiniz.