Tenho as seguintes classes:
As classes foram geradas pelo cmd xjc arquivo.xsd
Classe Carro:
package testexml;
import java.util.ArrayList;
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.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Carro", propOrder = {
"nome",
"portas",
"motoristas"
})
public class Carro {
@XmlElement(required = true)
protected String nome;
protected int portas;
protected List<Motorista> motoristas;
public String getNome() {
return nome;
}
public void setNome(String value) {
this.nome = value;
}
public int getPortas() {
return portas;
}
public void setPortas(int value) {
this.portas = value;
}
public List<Motorista> getMotoristas() {
if (motoristas == null) {
motoristas = new ArrayList<Motorista>();
}
return this.motoristas;
}
}
Classe Motorista:
package testexml;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Motorista", propOrder = {
"nome"
})
public class Motorista {
@XmlElement(required = true)
protected String nome;
public String getNome() {
return nome;
}
public void setNome(String value) {
this.nome = value;
}
}
Classe ObjectFactory:
package testexml;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.annotation.XmlElementDecl;
import javax.xml.bind.annotation.XmlRegistry;
import javax.xml.namespace.QName;
@XmlRegistry
public class ObjectFactory {
private final static QName _Carro_QNAME = new QName("", "carro");
public ObjectFactory() {
}
public Carro createCarro() {
return new Carro();
}
public Motorista createMotorista() {
return new Motorista();
}
@XmlElementDecl(namespace = "", name = "carro")
public JAXBElement<Carro> createCarro(Carro value) {
return new JAXBElement<Carro>(_Carro_QNAME, Carro.class, null, value);
}
}
Classe de teste:
package testexml;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBElement;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
public class TesteXML {
public static void main(String[] args) {
Motorista m = new Motorista();
m.setNome("Motorista1");
Motorista m2 = new Motorista();
m2.setNome("Motorista2");
Carro c = new Carro();
c.setNome("HB20");
c.setPortas(4);
c.getMotoristas().add(m);
c.getMotoristas().add(m2);
try {
JAXBContext context = JAXBContext.newInstance("testexml");
Marshaller marshaller = context.createMarshaller();
JAXBElement<Carro> element = new ObjectFactory().createCarro(c);
marshaller.marshal(element, System.out);
} catch (JAXBException ex) {
Logger.getLogger(TesteXML.class.getName()).log(Level.SEVERE, null, ex);
}
}
}
O xml gerado é este:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<carro>
<nome>HB20</nome>
<portas>4</portas>
<motoristas>
<nome>Motorista1</nome>
</motoristas>
<motoristas>
<nome>Motorista2</nome>
</motoristas>
</carro>
A lista de motorista está estranha, a saída certa não seria esta abaixo?
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<carro>
<nome>HB20</nome>
<portas>4</portas>
<motoristas>
<motorista>
<nome>Motorista1</nome>
</motorista>
<motorista>
<nome>Motorista2</nome>
</motorista>
</motoristas>
</carro>
O que fiz de errado?