Jak dodać atrybuty xml do JAXB adnotated class XmlElementWrapper?
Mam klasę z adnotacją XmlElementWrapper jak:
...
@XmlElementWrapper(name="myList")
@XmlElements({
@XmlElement(name="myElement") }
)
private List<SomeType> someList = new LinkedList();
... Ten kod tworzy XML jak
<myList>
<myElement> </myElement>
<myElement> </myElement>
<myElement> </myElement>
</myList>
Jak na razie dobrze.
Ale teraz muszę dodać atrybuty do znacznika listy, aby uzyskać XML jak
<myList number="2">
<myElement> </myElement>
<myElement> </myElement>
<myElement> </myElement>
</myList>
Czy istnieje ' inteligentny sposób, aby to osiągnąć bez tworzenia nowej klasy, która zawiera reprezentuje listę?
3 answers
Mam lepsze rozwiązanie na twoje pytanie.
Aby utworzyć obiekt XML Java, użyj następującego kodu:
import java.util.*;
import javax.xml.bind.annotation.*;
@XmlRootElement(name="myList")
public class Root {
private String number;
private List<String> someList;
@XmlAttribute(name="number")
public String getNumber() {
return number;
}
public void setNumber(String number) {
this.number = number;
}
@XmlElement(name="myElement")
public List<String> getSomeList() {
return someList;
}
public void setSomeList(List<String> someList) {
this.someList = someList;
}
public Root(String numValue,List<String> someListValue) {
this();
this.number = numValue;
this.someList = someListValue;
}
/**
*
*/
public Root() {
// TODO Auto-generated constructor stub
}
}
Aby uruchomić powyższy kod za pomocą JAXB, użyj następującego kodu:
import java.util.ArrayList;
import java.util.List;
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
List<String> arg = new ArrayList<String>();
arg.add("FOO");
arg.add("BAR");
Root root = new Root("123", arg);
JAXBContext jc = JAXBContext.newInstance(Root.class);
Marshaller marshaller = jc.createMarshaller();
marshaller.marshal(root, System.out);
}
}
Wygeneruje to następujący plik XML jako wyjście:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<myList number="123">
<myElement>FOO</myElement>
<myElement>BAR</myElement>
</myList>
Myślę, że to jest bardziej pomocne dla Ciebie.
Dzięki..Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-09-21 10:40:38
Implementacja MOXy JAXB (I ' m the tech lead) ma rozszerzenie (@XmlPath) do obsługi tej sprawy:
import java.util.*;
import javax.xml.bind.annotation.*;
import org.eclipse.persistence.oxm.annotations.XmlPath;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Root {
@XmlPath("myList/@number")
private int number;
@XmlElementWrapper(name="myList")
@XmlElement(name="myElement")
private List<String> someList = new LinkedList<String>();
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
public List<String> getSomeList() {
return someList;
}
public void setSomeList(List<String> someList) {
this.someList = someList;
}
}
Stworzy następujący XML:
<?xml version="1.0" encoding="UTF-8"?>
<root>
<myList number="123">
<myElement>FOO</myElement>
<myElement>BAR</myElement>
</myList>
</root>
Po uruchomieniu tego kodu:
import javax.xml.bind.*;
public class Demo {
public static void main(String[] args) throws Exception {
JAXBContext jc = JAXBContext.newInstance(Root.class);
Root root = new Root();
root.setNumber(123);
root.getSomeList().add("FOO");
root.getSomeList().add("BAR");
Marshaller marshaller = jc.createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
marshaller.marshal(root, System.out);
}
}
Aby to zadziałało przy użyciu ściśle standardowego kodu JAXB, musisz użyć adaptera XML:
Uwaga:
Aby użyć MOXy JAXB you trzeba dodać plik o nazwie jaxb.właściwości w klasach modelu z następującym wpisem:
javax.xml.bind.context.factory=org.eclipse.persistence.jaxb.JAXBContextFactory
Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2010-09-08 15:23:23
Jeśli nie używasz MOXy lub po prostu chcesz trzymać się standardowych adnotacji JAXB, możesz rozszerzyć odpowiedź Noby, aby dodać obsługę generycznej klasy wrapper. Noby ' s answer obsługuje obecnie tylko listę ciągów znaków, ale powiedzmy na przykład, że będziesz używać tej samej generycznej klasy wrapper dla kilku różnych klas. W moim przykładzie chcę stworzyć generyczną klasę "PagedList", która będzie wyglądać jak lista, ale będzie również zawierała informacje o przesunięciu strony oraz całkowitą liczbę elementów na liście nieoznaczonej.
Jedynym minusem tego rozwiązania jest to, że musisz dodać dodatkowe mapowania @XmlElement dla każdego typu klasy, która zostanie zawinięta. Ogólnie rzecz biorąc, prawdopodobnie lepsze rozwiązanie niż tworzenie nowej klasy dla każdego elementu pagable.
@XmlType
public class PagedList<T> {
@XmlAttribute
public int offset;
@XmlAttribute
public long total;
@XmlElements({
@XmlElement(name="order", type=Order.class),
@XmlElement(name="address", type=Address.class)
// additional as needed
})
public List<T> items;
}
@XmlRootElement(name="customer-profile")
public class CustomerProfile {
@XmlElement
public PagedList<Order> orders;
@XmlElement
public PagedList<Address> addresses;
}
Ten przykład da ci:
<customer-profile>
<order offset="1" total="100">
<order> ... </order>
<order> ... </order>
<order> ... </order>
...
</orders>
<addresses offset="1" total="5">
<address> ... </address>
<address> ... </address>
<address> ... </address>
<address> ... </address>
<address> ... </address>
<addresses>
</customer-profile>
Mam nadzieję, że to pomoże. To jest rozwiązanie, które postanowiłem przynajmniej.Warning: date(): Invalid date.timezone value 'Europe/Kyiv', we selected the timezone 'UTC' for now. in /var/www/agent_stack/data/www/doraprojects.net/template/agent.layouts/content.php on line 54
2011-12-30 17:12:07