闪灵乐队被禁:使用jaxb将XML转化为JAVA BEAN
来源:百度文库 编辑:九乡新闻网 时间:2024/04/27 19:12:12
直接贴代码了,其中的参数的意思,自己查查就知道了,或者用一下就明白了。
Java代码 ',1)">
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;
@XmlElement
public Resources resources;
@XmlElement
public HeaderItems headerItems;
@XmlElement
public Properties properties;
@XmlElement
public BodyItems bodyItems;
@XmlTransient
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlTransient
public String getTemplate() {
return template;
}
public void setTemplate(String template) {
this.template = template;
}
@XmlTransient
public Resources getResources() {
return resources;
}
public void setResources(Resources resources) {
this.resources = resources;
}
@XmlTransient
public HeaderItems getHeaderItems() {
return headerItems;
}
public void setHeaderItems(HeaderItems headerItems) {
this.headerItems = headerItems;
}
@XmlTransient
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
@XmlTransient
public BodyItems getBodyItems() {
return bodyItems;
}
public void setBodyItems(BodyItems bodyItems) {
this.bodyItems = bodyItems;
}
}
Moduel对象其中一个对象属性:
Java代码 ',2)">
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 fields;
@XmlTransient
public List getFields() {
return fields;
}
public void setFields(List fields) {
this.fields = fields;
for (int i=0; ifields.get(i).setHeaderItem(true);
}
public void clear() {
fields.clear();
}
}
Java代码 ',3)">
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;
@XmlElement
public 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;
}
@XmlTransient
public String getName() {
return name;
}
@XmlTransient
public String getType() {
return type;
}
@XmlTransient
public Entries getEntries() {
return this.entries;
}
@XmlTransient
public 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 getEntriesList() {
return entries.getEntryList();
}
}
最后XML闪亮登场:
Java代码 ',4)">
multiple='false' />
multiple='false' />
editable="false">
Metadata是一个抽象类,描述了些 ID NAME VALUE基本属性 没什么特别的
关于XML转java对象的操作:
Java代码 ',5)">
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/
Java代码 ',1)">
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;
@XmlElement
public Resources resources;
@XmlElement
public HeaderItems headerItems;
@XmlElement
public Properties properties;
@XmlElement
public BodyItems bodyItems;
@XmlTransient
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
@XmlTransient
public String getTemplate() {
return template;
}
public void setTemplate(String template) {
this.template = template;
}
@XmlTransient
public Resources getResources() {
return resources;
}
public void setResources(Resources resources) {
this.resources = resources;
}
@XmlTransient
public HeaderItems getHeaderItems() {
return headerItems;
}
public void setHeaderItems(HeaderItems headerItems) {
this.headerItems = headerItems;
}
@XmlTransient
public Properties getProperties() {
return properties;
}
public void setProperties(Properties properties) {
this.properties = properties;
}
@XmlTransient
public BodyItems getBodyItems() {
return bodyItems;
}
public void setBodyItems(BodyItems bodyItems) {
this.bodyItems = bodyItems;
}
}
Moduel对象其中一个对象属性:
Java代码 ',2)">
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
@XmlTransient
public List
return fields;
}
public void setFields(List
this.fields = fields;
for (int i=0; i
}
public void clear() {
fields.clear();
}
}
Java代码 ',3)">
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;
@XmlElement
public 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;
}
@XmlTransient
public String getName() {
return name;
}
@XmlTransient
public String getType() {
return type;
}
@XmlTransient
public Entries getEntries() {
return this.entries;
}
@XmlTransient
public 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
return entries.getEntryList();
}
}
最后XML闪亮登场:
Java代码 ',4)">
Metadata是一个抽象类,描述了些 ID NAME VALUE基本属性 没什么特别的
关于XML转java对象的操作:
Java代码 ',5)">
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/
使用jaxb将XML转化为JAVA BEAN
使用 JAXB 工具根据 Java 类生成 XML 模式
benafactory.xml bean工程配置文件
利用Digester把XML转换为Java对象
将孩子的谎言转化为智慧
使用 dom4j 解析 XML
使用 dom4j 解析 XML
将数字转化为拥有x位小数位的形式
酒店如何将前台转化为销售渠道?
将PPT文件转化为Word文档及最佳打印
亚洲“太阳能大黄蜂”能够将阳光转化为电能
视频表格中如何将姓名转化为拼音怎么办
亚洲“太阳能大黄蜂”能够将阳光转化为电能..
将情绪变化整合,转化为有机的沟通
使用Java Service Wrddddapper将Java程序发布成Windows Servi...
在使用Spring时不使用注入方式获取bean
关于将java项目导出为jar文件运行
使用JAXP处理XML文件
spring中使用oscache报Error creating bean(Requeste...
java把图片转换成base64传入XML文件
转化为灰度图
数字转化为字符串
VC解析XML--使用CMarkup类解析
使用 MSXML 分析器处理 XML 文档