我们专注攀枝花网站设计 攀枝花网站制作 攀枝花网站建设
成都网站建设公司服务热线:400-028-6601

网站建设知识

十年网站开发经验 + 多家企业客户 + 靠谱的建站团队

量身定制 + 运营维护+专业推广+无忧售后,网站问题一站解决

xml和bean之间转化

xml和bean之间转化
//bean
@XmlRootElement(name = "register")@XmlAccessorType(XmlAccessType.PROPERTY)
br/>@XmlAccessorType(XmlAccessType.PROPERTY)
public class User {
private String name;
private String age;
public String getName() {
return name;
}

创新互联建站主营八步网站建设的网络公司,主营网站建设方案,手机APP定制开发,八步h5成都微信小程序搭建,八步网站营销推广欢迎八步等地区企业咨询

public void setName(String name) {
    this.name = name;
}

public String getAge() {
    return age;
}

public void setAge(String age) {
    this.age = age;
}

@Override
public String toString() {
    return "User{" +
            "name='" + name + '\'' +
            ", age='" + age + '\'' +
            '}';
}

}bean反序列化xml:
@Test
br/>bean反序列化xml:
@Test
try {
User user=new User();
user.setName("zhan");
user.setAge("23");
JAXBContext context = JAXBContext.newInstance(User.class);
Marshaller marshaller = context.createMarshaller();
// 是否格式化
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
// 是否省略头文件
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, false);
StringWriter swriter = new StringWriter();
// 将XML内容打印为字符串
marshaller.marshal(user, swriter);

        String xmlString = swriter.getBuffer().toString();
        System.out.println(xmlString);
    }catch (Exception e){
        e.printStackTrace();
    }
}

xml序列化成bean:@Test
br/>@Test
try {
String xml="\n" +
"\n" +
" zhan\n" +
" 23\n" +
"
";
JAXBContext context = JAXBContext.newInstance(User.class);
Unmarshaller unmarshaller = context.createUnmarshaller();
Object unmarshal = unmarshaller.unmarshal(new StringReader(xml));
System.out.println(unmarshal.toString());
} catch (JAXBException e) {
e.printStackTrace();
}
}


网站名称:xml和bean之间转化
本文路径:http://shouzuofang.com/article/gccpdp.html

其他资讯