import javax.xml.bind.annotation.XmlElement;
public class Test1 {
private String Type;
private String value;
@XmlElement(name="type")
public final String getType(){
return Type;
}
public final void setType(final String type){
this.Type=type;
}
@XmlElement(name="value")
public final String getValue(){
return value;
}
public final void setValue(final String value) {
this.value = value;
}
}
import java.util.ArrayList;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlElementWrapper;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement(name="test")
public class Test2 {
ArrayList<Test1> test1;
@XmlElementWrapper(name="users")
@XmlElement(name="user")
public final ArrayList<Test1> getUser(){
return test1;
}
public final void setUser(final ArrayList<Test1> mm){
this.test1=mm;
}
}
import java.util.ArrayList;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Marshaller;
import com.covidien.laptopagent.xml.XMLUserLogin;
public class TEST {
public static void main(String agrs[]){
Test1 test1=new Test1();
test1.setType("100");
test1.setValue("none");
test1.setType("101");
test1.setValue("ok");
ArrayList<Test1> permfile=new ArrayList<Test1>();
permfile.add(test1);
ArrayList<Test2>test2 =new ArrayList<Test2>();
Test2 test2obj=new Test2();
test2obj.setuser(permfile);
JAXBContext context;
try {
context = JAXBContext.newInstance(XMLUserLogin.class);
Marshaller m = context.createMarshaller();
m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, Boolean.TRUE);
m.marshal(test2, System.out);
System.out.println();
} catch (JAXBException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
}
例外出力:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<test>
......
<users>
<user>
<type>100</type>
<value>none</value>
</user>
<user>
<type>101</type>
<value>ok</value>
</user>
</users>
...............
...............
</test>
ただし、user の 1 つの値のみを返します。
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<test>
......
<users>
<user>
<type>100</type>
<value>none</value>
</user>
</users>
...............
...............
</test>
これを解決するには?誰かが私を助けてくれますか?
ありがとう