-
Marshaller marshaller = context.createMarshaller();
-
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
-
marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
-
StringWriter writer = new StringWriter();
-
marshaller.marshal(obj, writer);
-
result = writer.toString();
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
return result;
-
}
-
/**
-
* xml转换成JavaBean
-
* @param xml
-
* @param c
-
* @return
-
*/
-
@SuppressWarnings(“unchecked”)
-
public static T converyToJavaBean(String xml, Class c) {
-
T t = null;
-
try {
-
JAXBContext context = JAXBContext.newInstance©;
-
Unmarshaller unmarshaller = context.createUnmarshaller();
-
t = (T) unmarshaller.unmarshal(new StringReader(xml));
-
} catch (Exception e) {
-
e.printStackTrace();
-
}
-
return t;
-
}
-
}
非常简单易懂,需要注意的是
-
marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
-
marshaller.setProperty(Marshaller.JAXB_ENCODING, encoding);
Marshaller.JAXB_FORMATTED_OUTPUT 决定是否在转换成xml时同时进行格式化(即按标签自动换行,否则即是一行的xml)
Marshaller.JAXB_ENCODING xml的编码方式
另外,Marshaller 还有其他Property可以设置,可以去查阅api。
2、最简单转换
-
package t1;
-
import java.util.Date;
-
import javax.xml.bind.annotation.XmlAccessType;
-
import javax.xml.bind.annotation.XmlAccessorType;
-
import javax.xml.bind.annotation.XmlAttribute;
-
import javax.xml.bind.annotation.XmlElement;
-
import javax.xml.bind.annotation.XmlRootElement;
-
import javax.xml.bind.annotation.XmlType;
-
/**
-
* @author zhuc
-
* @create 2013-3-29 下午2:49:48
-
*/
-
@XmlAccessorType(XmlAccessType.FIELD)
-
@XmlRootElement
-
@XmlType(name = “book”, propOrder = { “author”, “calendar”, “price”, “id” })
-
public class Book {
-
@XmlElement(required = true)
-
private String author;
-
@XmlElement(name = “price_1”, required = true)
-
private float price;
-
@XmlElement
-
private Date calendar;
-
@XmlAttribute
-
private Integer id;
-
/**
-
* @return the author
-
*/
-
public String getAuthor() {
-
return author;
-
}
-
/**
-
* @return the price
-
*/
-
public float getPrice() {
-
return price;
-
}
-
/**
-
* @return the calendar
-
*/
-
public Date getCalendar() {
-
return calendar;
-
}
-
/**
-
* @return the id
-
*/
-
public Integer getId() {
-
return id;
-
}
-
/**
-
* @param author the author to set
-
*/
-
public void setAuthor(String author) {
-
this.author = author;
-
}
-
/**
-
* @param price the price to set
-
*/
-
public void setPrice(float price) {
-
this.price = price;
-
}
-
/**
-
* @param calendar the calendar to set
-
*/
-
public void setCalendar(Date calendar) {
-
this.calendar = calendar;
-
}
-
/**
-
* @param id the id to set
-
*/
-
public void setId(Integer id) {
-
this.id = id;
-
}
-
/* (non-Javadoc)
-
* @see java.lang.Object#toString()
-
*/
-
@Override
-
public String toString() {
-
return “Book [author=” + author + “, price=” + price + “, calendar=” + calendar + “, id=” + id + “]”;
-
}
-
}
-
package t1;
-
import java.util.Date;
-
import javax.xml.bind.JAXBException;
-
import org.junit.Test;
-
import utils.JaxbUtil;
-
/**
-
* @author zhuc
-
* @create 2013-3-29 下午2:50:00
-
*/
-
public class JaxbTest1 {
-
/**
-
* @throws JAXBException
-
*/
-
@Test
-
public void showMarshaller() {
-
Book book = new Book();
-
book.setId(100);
-
book.setAuthor(“James”);
-
book.setCalendar(new Date());
-
book.setPrice(23.45f); //默认是0.0
-
String str = JaxbUtil.convertToXml(book);
-
System.out.println(str);
-
}
-
/**
-
* @throws JAXBException
-
*/
-
@Test
-
public void showUnMarshaller() {
-
String str = “<?xml version=\\"1.0\\" encoding=\\"UTF-8\\" standalone=\\"yes\\"?>” +
-
“<book id=\“100\”>” +
-
" James" +
-
" 2013-03-29T09:25:56.004+08:00" +
-
" <price_1>23.45</price_1>" +
-
“”;
-
Book book = JaxbUtil.converyToJavaBean(str, Book.class);
-
System.out.println(book);
-
}
-
}
输出结果分别为:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>< book id=“100”>
James
2013-03-29T14:50:58.974+08:00
<price_1>23.45</price_1>
< /book>
Book [author=James, price=23.45, calendar=Fri Mar 29 09:25:56 CST 2013, id=100]
3、类中包含复杂对象的转换
-
package t2;
-
import javax.xml.bind.annotation.XmlAccessType;
-
import javax.xml.bind.annotation.XmlAccessorType;
-
import javax.xml.bind.annotation.XmlAttribute;
-
import javax.xml.bind.annotation.XmlElement;
-
import javax.xml.bind.annotation.XmlRootElement;
-
import javax.xml.bind.annotation.XmlType;
-
/**
-
* @author zhuc
-
* @create 2013-3-29 下午2:51:44
-
*/
-
@XmlAccessorType(XmlAccessType.FIELD)
-
@XmlRootElement(name = “student”)
-
@XmlType(propOrder = {})
-
public class Student {
-
@XmlAttribute
-
private Integer id;
-
@XmlElement
-
private String name;
-
@XmlElement(name = “role”)
-
private Role role;
-
/**
-
* @return the id
-
*/
-
public Integer getId() {
-
return id;
-
}
-
/**
-
* @return the name
-
*/
-
public String getName() {
-
return name;
-
}
-
/**
-
* @return the role
-
*/
-
public Role getRole() {
-
return role;
-
}
-
/**
-
* @param id the id to set
-
*/
-
public void setId(Integer id) {
-
this.id = id;
-
}
-
/**
-
* @param name the name to set
-
*/
-
public void setName(String name) {
-
this.name = name;
-
}
-
/**
-
* @param role the role to set
-
*/
-
public void setRole(Role role) {
-
this.role = role;
-
}
-
/* (non-Javadoc)
-
* @see java.lang.Object#toString()
-
*/
-
@Override
-
public String toString() {
-
return “Student [id=” + id + “, name=” + name + “, role=” + role + “]”;
-
}
-
}
-
package t2;
-
import javax.xml.bind.annotation.XmlAccessType;
-
import javax.xml.bind.annotation.XmlAccessorType;
-
import javax.xml.bind.annotation.XmlElement;
-
import javax.xml.bind.annotation.XmlType;
-
/**
-
* @author zhuc
-
* @create 2013-3-29 下午2:51:52
-
*/
-
@XmlAccessorType(XmlAccessType.FIELD)
-
@XmlType(propOrder = { “name”, “desc” })
-
public class Role {
-
@XmlElement
-
private String name;
-
@XmlElement
-
private String desc;
-
/**
-
* @return the name
-
*/
-
public String getName() {
-
return name;
-
}
-
/**
-
* @return the desc
-
*/
-
public String getDesc() {
-
return desc;
-
}
-
/**
-
* @param name the name to set
-
*/
-
public void setName(String name) {
-
this.name = name;
-
}
-
/**
-
* @param desc the desc to set
-
*/
-
public void setDesc(String desc) {
-
this.desc = desc;
-
}
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。
深知大多数Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则几千的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!
因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
最后
下面是有几位Android行业大佬对应上方技术点整理的一些进阶资料。希望能够帮助到大家提升技术
高级UI,自定义View
UI这块知识是现今使用者最多的。当年火爆一时的Android入门培训,学会这小块知识就能随便找到不错的工作了。
不过很显然现在远远不够了,拒绝无休止的CV,亲自去项目实战,读源码,研究原理吧!
9147)]
[外链图片转存中…(img-SzO2kCrL-1711777349148)]
[外链图片转存中…(img-vB5M0UMr-1711777349148)]
[外链图片转存中…(img-jlcWuqst-1711777349148)]
既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!
由于文件比较大,这里只是将部分目录大纲截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且后续会持续更新
如果你觉得这些内容对你有帮助,可以添加V获取:vip204888 (备注Android)
[外链图片转存中…(img-L4riGy7c-1711777349149)]
最后
下面是有几位Android行业大佬对应上方技术点整理的一些进阶资料。希望能够帮助到大家提升技术
[外链图片转存中…(img-22R2EHDe-1711777349149)]
高级UI,自定义View
UI这块知识是现今使用者最多的。当年火爆一时的Android入门培训,学会这小块知识就能随便找到不错的工作了。
不过很显然现在远远不够了,拒绝无休止的CV,亲自去项目实战,读源码,研究原理吧!
[外链图片转存中…(img-Q9Ja39nu-1711777349149)]