Come posso generare il seguente XML usando JAXB?
sport description
Annotare tipo e proprietà di genere con @XmlAttribute
e la proprietà description con @XmlValue
:
package org.example.sport; import javax.xml.bind.annotation.*; @XmlAccessorType(XmlAccessType.FIELD) @XmlRootElement public class Sport { @XmlAttribute protected String type; @XmlAttribute protected String gender; @XmlValue; protected String description; }
Per maggiori informazioni
Lo schema corretto dovrebbe essere:
< ?xml version="1.0" encoding="UTF-8"?>
Il codice generato per SportType sarà:
package org.example.sport; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlType; @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "sportType") public class SportType { @XmlValue protected String value; @XmlAttribute protected String type; @XmlAttribute protected String gender; public String getValue() { return value; } public void setValue(String value) { this.value = value; } public String getType() { return type; } public void setType(String value) { this.type = value; } public String getGender() { return gender; } public void setGender(String value) { this.gender = value; } }
Ecco una soluzione funzionante:
Produzione:
public class XmlTest { private static final Logger log = LoggerFactory.getLogger(XmlTest.class); @Test public void createDefaultBook() throws JAXBException { JAXBContext jaxbContext = JAXBContext.newInstance(Book.class); Marshaller marshaller = jaxbContext.createMarshaller(); StringWriter writer = new StringWriter(); marshaller.marshal(new Book(), writer); log.debug("Book xml:\n {}", writer.toString()); } @XmlAccessorType(XmlAccessType.FIELD) @XmlRootElement(name = "book") public static class Book { @XmlElementRef(name = "price") private Price price = new Price(); } @XmlAccessorType(XmlAccessType.FIELD) @XmlRootElement(name = "price") public static class Price { @XmlAttribute(name = "drawable") private Boolean drawable = true; //you may want to set default value here @XmlValue private int priceValue = 1234; public Boolean getDrawable() { return drawable; } public void setDrawable(Boolean drawable) { this.drawable = drawable; } public int getPriceValue() { return priceValue; } public void setPriceValue(int priceValue) { this.priceValue = priceValue; } } }
Produzione:
22: 00: 18.471 [principale] DEBUG com.grebski.stack.XmlTest – Book xml:
< ?xml version="1.0" encoding="UTF-8" standalone="yes"?> 1234
Soluzione aggiornata: utilizzo della soluzione dello schema di cui discutevamo. Questo ti porta alla tua risposta:
Schema di esempio:
< ?xml version="1.0" encoding="UTF-8"?>
Codice generato
SportType:
package org.example.sport; import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlAttribute; import javax.xml.bind.annotation.XmlType; @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "sportType") public class SportType { @XmlAttribute protected String type; @XmlAttribute protected String gender; public String getType() { return type; } public void setType(String value) { this.type = value; } public String getGender() { return gender; } public void setGender(String value) { this.gender = value; } }
Gli sport:
package org.example.sport; 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.XmlRootElement; import javax.xml.bind.annotation.XmlType; @XmlAccessorType(XmlAccessType.FIELD) @XmlType(name = "", propOrder = { "sport" }) @XmlRootElement(name = "sports") public class Sports { protected List sport; public List getSport() { if (sport == null) { sport = new ArrayList (); } return this.sport; } }
I file di class di output vengono prodotti eseguendo xjc sullo schema nella riga di comando