Reputation: 43
I have a not-little problem with my current project, and probably you can help me with this...
When I worked before with xml messages, marshalling converted java objects to xml, where the java object attributes were set to xml nodes. But now I need to set that object attributes to xml node attributes, as I show here:
<Identification_List counter="">
<Identification number="XXXXXXX" letter="X" name="PERSON">
<TravelList counter="">
<Travel travelType="">
</TravelList>
</Identification>
</Identification_List>
How can I do this? What framework should I use? Thank you!
Edit: Example java class:
public class Identification {
private int number;
private char letter;
private String name;
private List<Travel> travelList;
//Add here constructors, getters and setters
}
That java class is the one that should be marshalled, where number, letter and name are the xml object properties
Upvotes: 0
Views: 6786
Reputation:
Do you need to convert java to xml ? use the same library ,
Here an Example
1-IdentificationList Class
package test;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.List;
@XmlRootElement(name = "Identification_List")
public class IdentificationList {
private int counter;
private List<Identification> identificationList;
public IdentificationList() {
}
public IdentificationList(List<Identification> identificationList) {
this.identificationList = identificationList;
this.counter = identificationList == null ? 0 : identificationList.size();
;
}
@XmlElement(name = "Identification")
public List<Identification> getIdentificationList() {
return identificationList;
}
public void setIdentificationList(List<Identification> identificationList) {
this.identificationList = identificationList;
}
@XmlAttribute
public int getCounter() {
return counter;
}
public void setCounter(int counter) {
this.counter = counter;
}
}
2-Identification Class
package test;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlType;
@XmlType(propOrder = {"number", "letter", "name","travelList"})
public class Identification {
private int number;
private String letter;
private String name;
private TravelList travelList;
public Identification() {
}
public Identification(int number, String letter, String name, TravelList travelList) {
this.number = number;
this.letter = letter;
this.name = name;
this.travelList = travelList;
}
@XmlAttribute
public int getNumber() {
return number;
}
public void setNumber(int number) {
this.number = number;
}
@XmlAttribute
public String getLetter() {
return letter;
}
public void setLetter(String letter) {
this.letter = letter;
}
@XmlAttribute
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlElement(name = "TravelList")
public TravelList getTravelList() {
return travelList;
}
public void setTravelList(TravelList travelList) {
this.travelList = travelList;
}
}
3-TravelList Class
package test;
import javax.xml.bind.annotation.XmlAttribute;
import javax.xml.bind.annotation.XmlElement;
import java.util.List;
public class TravelList {
private List<Travel>travels;
private int counter;
public TravelList() {
}
public TravelList(List<Travel> travels) {
this.travels = travels;
this.counter=travels==null?0:travels.size();
}
@XmlElement(name = "Travel")
public List<Travel> getTravels() {
return travels;
}
public void setTravels(List<Travel> travels) {
this.travels = travels;
}
@XmlAttribute
public int getCounter() {
return counter;
}
public void setCounter(int counter) {
this.counter = counter;
}
}
4-Travel Class
package test;
import javax.xml.bind.annotation.XmlAttribute;
public class Travel {
private String travelType;
public Travel() {
}
public Travel(String travelType) {
this.travelType = travelType;
}
@XmlAttribute
public String getTravelType() {
return travelType;
}
public void setTravelType(String travelType) {
this.travelType = travelType;
}
}
5-IdentificationToXML Class
package test;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.Marshaller;
import java.io.FileOutputStream;
import java.util.ArrayList;
public class IdentificationToXML {
public static void main(String ...args) throws Exception {
JAXBContext contextObj = JAXBContext.newInstance(IdentificationList.class);
Marshaller marshallerObj = contextObj.createMarshaller();
marshallerObj.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
Travel travel1=new Travel("My First Travel");
Travel travel2=new Travel("My Second Travel");
Travel travel3=new Travel("My Third Travel");
Travel travel4=new Travel("My Fourth Travel");
ArrayList<Travel> list=new ArrayList<Travel>();
list.add(travel1);
list.add(travel2);
ArrayList<Travel> list2=new ArrayList<Travel>();
list2.add(travel3);
list2.add(travel4);
Identification identification1=new Identification(111,"c","My Name",new TravelList(list));
Identification identification2=new Identification(222,"d","My Name",new TravelList(list2));
ArrayList<Identification> list3=new ArrayList<Identification>();
list3.add(identification1);
list3.add(identification2);
IdentificationList identification=new IdentificationList(list3);
marshallerObj.marshal(identification, new FileOutputStream("identification.xml"));
}
}
6-Output :
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<Identification_List counter="2">
<Identification number="111" letter="c" name="My Name">
<TravelList counter="2">
<Travel travelType="My First Travel"/>
<Travel travelType="My Second Travel"/>
</TravelList>
</Identification>
<Identification number="222" letter="d" name="My Name">
<TravelList counter="2">
<Travel travelType="My Third Travel"/>
<Travel travelType="My Fourth Travel"/>
</TravelList>
</Identification>
</Identification_List>
Upvotes: 1
Reputation: 206876
You need to add the appropriate JAXB annotations to your classes, to tell JAXB how to map your class to XML. For example:
@XmlRootElement(name = "Identification_List")
@XmlAccessorType(XmlAccessType.FIELD)
public class IdentificationList {
@XmlAttribute
private int counter;
@XmlElement(name = "Identification")
private List<Identification> identifications;
// getters and setters
}
@XmlAccessorType(XmlAccessType.FIELD)
public class Identification {
@XmlAttribute
private int number;
@XmlAttribute
private char letter;
@XmlAttribute
private String name;
@XmlElementWrapper(name = "TravelList")
@XmlElement(name = "Travel")
private List<Travel> travels;
// getters and setters
}
@XmlAccessorType(XmlAccessType.FIELD)
public class Travel {
@XmlAttribute
private String travelType;
// getters and setters
}
Upvotes: 1