您可以执行以下选项之一:
选项1
资料
package forum11311374;import java.util.List;import javax.xml.bind.annotation.*;@XmlRootElement@XmlAccessorType(XmlAccessType.FIELD)public class Datas { private List<String> data; //get/set methods}
想要查询更多的信息
- http://blog.bdoughan.com/2010/09/jaxb-collection-properties.html
选项#2
资料
package forum11311374;import java.util.List;import javax.xml.bind.annotation.*;@XmlRootElement@XmlAccessorType(XmlAccessType.FIELD)public class Datas { @XmlElement(name="data") private List<Data> datas; //get/set methods}
数据
package forum11311374;import javax.xml.bind.annotation.*;@XmlAccessorType(XmlAccessType.FIELD)public class Data{ @XmlValue private String data; //get/set methods}
想要查询更多的信息
- http://blog.bdoughan.com/2011/06/jaxb-and-complex-types-with-simple.html
以下选项可以与两个选项一起使用:
input.xml /输出
我已经更新了XML文档,使其包含必要的结束标记。
<data>apple</data>代替
<data>apple<data>。
<datas> <data>apple</data> <data>banana</data> <data>orange</data> </datas>
演示版
package forum11311374;import java.io.File;import javax.xml.bind.*;public class Demo { public static void main(String[] args) throws Exception { JAXBContext jc = JAXBContext.newInstance(Datas.class); Unmarshaller unmarshaller = jc.createUnmarshaller(); File xml = new File("src/forum11311374/input.xml"); Datas datas = (Datas) unmarshaller.unmarshal(xml); Marshaller marshaller = jc.createMarshaller(); marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true); marshaller.marshal(datas, System.out); }}
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)