xml to object java_java object与xml的转换

本文介绍了如何使用Java的JAXB库进行XML与Java对象之间的转换,包括不同注解的使用,如@XmlRootElement、@XmlAccessorType、@XmlElement等,以及命名空间和类型适配器的应用。
摘要由CSDN通过智能技术生成

概述:以前对于XML与Java对象的转换了解比较少,今天学微信接口API时刚好接触到,所以就写下来了,初学者望大家见谅哈。

1.既然是Java对象与XML的转换,所以就需要有个Java类来获得对象,本例子主要涉及到BOY类和测试运行的类

2.代码

2.1 开始第一步的简单学习

@XmlRootElement

@XmlAccessorType(XmlAccessType.FIELD)//field只是类上的字段,并不是属性

public class Boy {

String name = "xu**";

}

public class TestXml {

public static void  main(String args[]){

try {

JAXBContext jaxbContext = JAXBContext.newInstance(Boy.class);

Marshaller marshaller = jaxbContext.createMarshaller(); //将对象转换成XML格式

Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); //将XML转换成对象

Boy boy = new Boy();

marshaller.marshal(boy,System.out);//以打印输出的形式显示

System.out.println();

String xmlStr = "许**";

Boy towBoy = (Boy)unmarshaller.unmarshal(new StringReader(xmlStr));

System.out.println(towBoy.name);

} catch (JAXBException e) {

e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.

}

}

}

打印的结果是:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>xu**

许**

2.2 当boy变成下面这样子

@XmlRootElement

@XmlAccessorType(XmlAccessType.PROPERTY)//property是属性

public class Boy {

String name = "xu**";

}

打印的结果是:

xu**

2.3 想要有2.1的结果就需要提供name的get方法来获得属性

@XmlRootElement

@XmlAccessorType(XmlAccessType.PROPERTY)

public class Boy {

String name ="123";

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

2.4 也可以通过xmlelement注解

@XmlRootElement

@XmlAccessorType(XmlAccessType.PROPERTY)

public class Boy {

String name ="123";

@XmlElement

Integer age = 10;

public String getName() {

return name;

}

public void setName(String name) {

this.name = name;

}

}

打印的结果是:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>10123

许**

2.5 同时也可以将BOY改标签,并放在一个命名空间下

@XmlRootElement (name = "xu",namespace = "http://test")

@XmlAccessorType(XmlAccessType.PROPERTY)

public class Boy {

@XmlElement

String name ="123";

@XmlElement

Integer age = 10;

}

public class TestXml {

public static void  main(String args[]){

try {

JAXBContext jaxbContext = JAXBContext.newInstance(Boy.class);

Marshaller marshaller = jaxbContext.createMarshaller(); //将对象转换成XML格式

Unmarshaller unmarshaller = jaxbContext.createUnmarshaller(); //将XML转换成对象

Boy boy = new Boy();

marshaller.marshal(boy,System.out);

System.out.println();

String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>许**25";

Boy towBoy = (Boy)unmarshaller.unmarshal(new StringReader(xmlStr));

System.out.println(towBoy.name);

System.out.println(towBoy.age);

} catch (JAXBException e) {

e.printStackTrace();  //To change body of catch statement use File | Settings | File Templates.

}

}

}

打印的结果是:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>12310

许**

25

2.6 @XmlJavaTypeAdaptor

@XmlRootElement (name = "xu",namespace = "http://test")

@XmlAccessorType(XmlAccessType.PROPERTY)

public class Boy {

@XmlElement

String name ="许**";

@XmlElement

Integer age = 25;

private TestXmlInterface testXmlInterface;

}

我们要多写一个类TestXmlInterfaceAdaptor用来返回TestXmlInterface的一个具体实现类的推向

在转换成XML时接口TestXmlInterface 无法被转换,我们得加上@XmlJavaTypeAdaptor(TestXmlInterfaceAdaptor.class)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值