概述:之前對於XML與Java對象的轉換了解比較少,今天學微信接口API時恰好接觸到,因此就寫下來了,初學者望你們見諒哈。微信
1.既然是Java對象與XML的轉換,因此就須要有個Java類來得到對象,本例子主要涉及到BOY類和測試運行的類ide
2.代碼學習
2.1 開始第一步的簡單學習
測試
@XmlRootElementthis
@XmlAccessorType(XmlAccessType.FIELD)//field只是類上的字段,並非屬性spa
public class Boy {orm
String name = "xu**";xml
}對象
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><name>許**</name></boy>";
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"?><boy><name>xu**</name></boy>
許**
2.2 當boy變成下面這樣子
@XmlRootElement
@XmlAccessorType(XmlAccessType.PROPERTY)//property是屬性
public class Boy {
String name = "xu**";
}
打印的結果是:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?><boy/>
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"?><boy><age>10</age><name>123</name></boy>
許**
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\"?><ns2:xu xmlns:ns2=\"http://test\"><name>許**</name><age>25</age></ns2:xu>";
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"?><ns2:xu xmlns:ns2="http://test"><name>123</name><age>10</age></ns2:xu>
許**
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)