I got a better solution for your question.
For making Xml Java object, use the following code:
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
}
}
To run the above code using JAXB, use the following:
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);
}
}
This will produce the following XML as the output:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<myList number="123">
<myElement>FOO</myElement>
<myElement>BAR</myElement>
</myList>
I think this is more helpful you.
Thanks..
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…