awdxzc 发表于 2013-2-5 10:11:49

使用jaxb将XML转化为JAVA BEAN

直接贴代码了,其中的参数的意思,自己查查就知道了,或者用一下就明白了。
import java.util.ArrayList;import java.util.List;import javax.xml.bind.annotation.XmlAttribute;import javax.xml.bind.annotation.XmlElement;import javax.xml.bind.annotation.XmlRootElement;import javax.xml.bind.annotation.XmlTransient;@XmlRootElement(name="module")public class Module extends Metadata{@XmlAttribute(name="name")public String name;@XmlAttribute(name="template")public String template;@XmlElementpublic Resources resources;@XmlElementpublic HeaderItems headerItems;@XmlElementpublic Properties properties;@XmlElementpublic BodyItems bodyItems;@XmlTransientpublic String getName() {return name;}public void setName(String name) {this.name = name;}@XmlTransientpublic String getTemplate() {return template;}public void setTemplate(String template) {this.template = template;}@XmlTransientpublic Resources getResources() {return resources;}public void setResources(Resources resources) {this.resources = resources;}@XmlTransientpublic HeaderItems getHeaderItems() {return headerItems;}public void setHeaderItems(HeaderItems headerItems) {this.headerItems = headerItems;}@XmlTransientpublic Properties getProperties() {return properties;}public void setProperties(Properties properties) {this.properties = properties;}@XmlTransientpublic BodyItems getBodyItems() {return bodyItems;}public void setBodyItems(BodyItems bodyItems) {this.bodyItems = bodyItems;}}
Moduel对象其中一个对象属性:
import java.util.List;import javax.xml.bind.annotation.XmlElement;import javax.xml.bind.annotation.XmlTransient;public class HeaderItems extends Metadata {@XmlElement(name="field")private List<Field> fields;@XmlTransientpublic List<Field> getFields() {return fields;}public void setFields(List<Field> fields) {this.fields = fields;for (int i=0; i<fields.size(); i++)fields.get(i).setHeaderItem(true);} public void clear() {fields.clear();}}
package com.morningstar.wfe.metadata.node;import java.util.List;import javax.xml.bind.annotation.XmlAttribute;import javax.xml.bind.annotation.XmlElement;import javax.xml.bind.annotation.XmlTransient;public class Map extends Datasource {@XmlAttribute(name="name")public String name;@XmlAttribute(name="type")public String type;@XmlAttribute(name="url")public String url;@XmlElementpublic Entries entries;public void setName(String name) {this.name = name;}public void setType(String type) {this.type = type;}public void setEntries(Entries entries) {this.entries = entries;}@XmlTransientpublic String getName() {return name;}@XmlTransientpublic String getType() {return type;}@XmlTransientpublic Entries getEntries() {return this.entries;}@XmlTransientpublic String getUrl() {return url;}public void setUrl(String url) {this.url = url;}public int size() {if (entries == null) return 0;return entries.size();}public Entry get(int index) {if (entries == null) return null;return entries.get(index);}public List<Entry> getEntriesList() {return entries.getEntryList();}}
最后XML闪亮登场:
<?xml version="1.0" encoding="UTF-8"?><module name='showcase' template='showcase.ftl'><resources><map name='css' type='static'><entries><entry label="text_a1" value="text_a1"></entry><entry label="text_a2" value="text_a2"></entry><entry label="text_a3" value="text_a3"></entry><entry label="text_c1" value="text_c1"></entry><entry label="text_c2" value="text_c2"></entry></entries></map><map name='status' type='ws' url="xxxx"></map><map name='author' type='file' url="xxxx"></map></resources><headerItems><field name='active1' type='checkbox' datasource='css'multiple='false' /><field name='title1' type='text' format="string" required="true"/><field name='radio11' type='radio' datasource='css'/><field name='url11' type='url' format="string"/><field name='dropdownlist111' type='select' datasource='css' multiple="true" height="80" width="100"/><field name='title22' type='textarea' width="200" height="100" spellCheck="true" format="string" required="true"/></headerItems><properties><field name='active12' type='checkbox' datasource='status'multiple='false' /><field name='title12' type='date' format="int" /><field name='email11' type='email' visible="true" /></properties><bodyItems><field label="Name" name="name" type="text" visible="false"editable="false"></field><field name="readings_list" type="datatable" action="insert,remove"><columns><field label="Title" name="rSource" type="text" visible="true" width="30"/><field label="URL" name="rURL" type="text" visible="true" width="40"/><field label="Date" name="rDate" type="text" visible="true" width="50"/><field label="Source" name="rSource" type="text" visible="true" width="60"/></columns></field></bodyItems></module>

Metadata是一个抽象类,描述了些 ID NAME VALUE基本属性 没什么特别的

关于XML转java对象的操作:
public static Module parse(String metadata) throws MetadataParserException {Module module = null;try {JAXBContext jaxbContext = JAXBContext.newInstance(Module.class);Unmarshaller um = jaxbContext.createUnmarshaller();module = (Module)um.unmarshal(new ByteArrayInputStream(metadata.getBytes()));} catch (JAXBException e) {log.warn("JAXB castor failed to convert the metadata to module instance by {}",e.getMessage());throw new MetadataParserException();}return module;}

这样的话传进XML的字符串,就可以解析出这个JAVA对象。完全不用写任何过多的代码。岂不爽哉?!!!!

附一个java bean <=> xml 的unmi同志原创文档,主要是比较Jaxb和castor。


提供一个JAXB学习的地址:http://jaxb.java.net/
页: [1]
查看完整版本: 使用jaxb将XML转化为JAVA BEAN