资讯

精准传达 • 有效沟通

从品牌网站建设到网络营销策划,从策略到执行的一站式服务

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://cdkjz.cn/article/gccpdp.html
多年建站经验

多一份参考,总有益处

联系快上网,免费获得专属《策划方案》及报价

咨询相关问题或预约面谈,可以通过以下方式与我们联系

大客户专线   成都:13518219792   座机:028-86922220