成都创新互联网站制作重庆分公司

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之间转化
标题URL:http://cxhlcq.com/article/gccpdp.html

其他资讯

在线咨询

微信咨询

电话咨询

028-86922220(工作日)

18980820575(7×24)

提交需求

返回顶部