栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 面试经验 > 面试问答

使用POJO和JAXB批注绑定XML

面试问答 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

使用POJO和JAXB批注绑定XML

您可以执行以下选项之一:

选项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);    }}


转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/498650.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号