- 浏览: 111935 次
- 性别:
- 来自: 广州
文章分类
最新评论
-
ron.luo:
干货,必须得顶。
JAXB使用经验总结 -
csdn_zuoqiang:
能否看下DWR的配置情况?谢谢
结合webservice实现dwr推送 -
友友水:
。。。。不好意思,无心之失,删不掉前一条评论
JAXB使用经验总结 -
友友水:
[/flash][/flash][/flash][/flash ...
JAXB使用经验总结 -
lihong11:
大哥,加加注释好不?看不懂唉
小玩dwr实现服务器推送
使用JAXB首先要知道它是干什么的
当然,切入正题
很多时候我们需要把认知世界转化为我们熟悉的java对象,以供方便操作。这里,JAXB可以把xml对象转化为我们的java对象,也可以把java对象转化为xml对象。这时候我们就得知道它的两个转化方法。
一个是unmarshal(),一个是marshal()
unmarshal()是把xml对象转化为我们需要的java对象的方法,自然marshal()是把java对象转化为xml对象的一个过程。
我们需要的估计就是这两个方法的精华,只需要用到这么多就可以完成很多的事情了。下面看代码:
这里templateFile 和 template 都是xml文件对象,这样的两个overload method就可以完成由xml对象转化为java对象了。
marshal()方法也贡献上。
方法都具备了 可以开工了。
工具方法都上齐了,你就可以在你需要的地方调用了。当然,我还没讲我们的java操作对象。
下面举个例子,比如上面出现的chart 就是我们需要操纵的对象,它可能是一个xml对象,而我们需要操作的是java对象,晕了吧:
比如我们的chart是在页面需要显示的图形,比如fusioncharts 的某个图,先定义为chart,它会是一个接口类,是所有xxxChart的父类。 比如我的具体chart名叫GaugeChart。
代码如下,别说晕说郁闷了:
上面那一堆的注解来自jaxb的jar包,下哪些包我就不说了。估计应该能看懂个所以然吧,那些属性都是这个gaugeChart所需要的图形显示属性,不必去细究,它需要什么,我们按上面添加就是了,不过貌似没有get()方法,不需要这个。里面注解的像这样的@XmlElement(name = "annotationGroup") ,这个annotationGroup也是像GaugeChart这样的java对象,需要被定义。
接下来我们可以在action中调用了。
这样的一个方法返回的就是一个xml的string型对象了,我们只需要在页面拿到这个string型的xml ,就可以通过fusioncharts(当然是我这里用了fusioncharts)来调用它并显示相应的图形了。
当然,切入正题
很多时候我们需要把认知世界转化为我们熟悉的java对象,以供方便操作。这里,JAXB可以把xml对象转化为我们的java对象,也可以把java对象转化为xml对象。这时候我们就得知道它的两个转化方法。
一个是unmarshal(),一个是marshal()
unmarshal()是把xml对象转化为我们需要的java对象的方法,自然marshal()是把java对象转化为xml对象的一个过程。
我们需要的估计就是这两个方法的精华,只需要用到这么多就可以完成很多的事情了。下面看代码:
private static <T> T unmarshal(File templateFile, JAXBContext context) throws JAXBException { final Unmarshaller unmarshaller = context.createUnmarshaller(); // Unmarshal the XML in the stringWriter back into an object final T chart = (T) unmarshaller.unmarshal(templateFile); return chart; } @SuppressWarnings("unchecked") private static <T> T unmarshal(String template, JAXBContext context) throws JAXBException { final Unmarshaller unmarshaller = context.createUnmarshaller(); // Unmarshal the XML in the stringWriter back into an object final T chart = (T) unmarshaller.unmarshal(new StringReader(template)); return chart; }
这里templateFile 和 template 都是xml文件对象,这样的两个overload method就可以完成由xml对象转化为java对象了。
private static <T> String marshal(JAXBContext context, final T chart) throws JAXBException { // Create the marshaller, this is the nifty little thing that will // actually transform the object into XML final Marshaller marshaller = context.createMarshaller(); // Create a stringWriter to hold the XML final StringWriter stringWriter = new StringWriter(); // Marshal the javaObject and write the XML to the stringWriter marshaller.marshal(chart, stringWriter); String chartXml = stringWriter.toString(); return chartXml; }
marshal()方法也贡献上。
方法都具备了 可以开工了。
public static <T> String process(File templateFile, Class<T> type, ChartFiller<T> filler) { String chartXml = null; try { JAXBContext context = JAXBContext.newInstance(type); final T chart = (T) unmarshal(templateFile, context); filler.fill(chart); chartXml = marshal(context, chart); } catch (JAXBException e) { log.error(e.getMessage(), e); } return chartXml; } public static <T> String process(String template, Class<T> type, ChartFiller<T> filler) { String chartXml = null; try { JAXBContext context = JAXBContext.newInstance(type); final T chart = (T) unmarshal(template, context); filler.fill(chart); chartXml = marshal(context, chart); } catch (JAXBException e) { log.error(e.getMessage(), e); } return chartXml; } public interface ChartFiller<T> { void fill(T chart); }
工具方法都上齐了,你就可以在你需要的地方调用了。当然,我还没讲我们的java操作对象。
下面举个例子,比如上面出现的chart 就是我们需要操纵的对象,它可能是一个xml对象,而我们需要操作的是java对象,晕了吧:
比如我们的chart是在页面需要显示的图形,比如fusioncharts 的某个图,先定义为chart,它会是一个接口类,是所有xxxChart的父类。 比如我的具体chart名叫GaugeChart。
代码如下,别说晕说郁闷了:
public interface Chart { }
@XmlRootElement(name = "chart") public class GaugeChart implements Chart { @XmlAttribute private String lowerLimit; @XmlAttribute private String upperLimit; @XmlAttribute private String lowerLimitDisplay; @XmlAttribute private String upperLimitDisplay; @XmlAttribute private String majorTMNumber; @XmlAttribute private String majorTMColor; @XmlAttribute private String majorTMHeight; @XmlAttribute private String minorTMNumber; @XmlAttribute private String minorTMColor; @XmlAttribute private String minorTMHeight; @XmlAttribute private String displayValueDistance; @XmlAttribute private String tickValueDistance; @XmlAttribute private String gaugeStartAngle; @XmlAttribute private String gaugeEndAngle; @XmlAttribute private String palette; @XmlAttribute private String showValue; @XmlAttribute private String numberSuffix; @XmlElementWrapper(name = "colorRange") @XmlElement(name = "color") private List<Color> colors; @XmlElementWrapper(name = "dials") @XmlElement(name = "dial") private List<Dial> dials; @XmlElementWrapper(name = "annotations") @XmlElement(name = "annotationGroup") private List<AnnotationGroup> annotations; public void addAnnotationGroup(AnnotationGroup annotationGroup) { if (annotations == null) annotations = new LinkedList<AnnotationGroup>(); this.annotations.add(annotationGroup); } public void addDials(Dial dial) { if (dials == null) dials = new LinkedList<Dial>(); this.dials.add(dial); } public void setAnnotations(List<AnnotationGroup> annotations) { this.annotations = annotations; } public void setDials(List<Dial> dials) { this.dials = dials; } public void setColors(List<Color> colors) { this.colors = colors; } public void addColor(Color color) { if (color == null) colors = new LinkedList<Color>(); this.colors.add(color); } public void setLowerLimit(String lowerLimit) { this.lowerLimit = lowerLimit; } public void setUpperLimit(String upperLimit) { this.upperLimit = upperLimit; } public void setLowerLimitDisplay(String lowerLimitDisplay) { this.lowerLimitDisplay = lowerLimitDisplay; } public void setUpperLimitDisplay(String upperLimitDisplay) { this.upperLimitDisplay = upperLimitDisplay; } public void setMajorTMNumber(String majorTMNumber) { this.majorTMNumber = majorTMNumber; } public void setMajorTMColor(String majorTMColor) { this.majorTMColor = majorTMColor; } public void setMajorTMHeight(String majorTMHeight) { this.majorTMHeight = majorTMHeight; } public void setMinorTMNumber(String minorTMNumber) { this.minorTMNumber = minorTMNumber; } public void setMinorTMColor(String minorTMColor) { this.minorTMColor = minorTMColor; } public void setMinorTMHeight(String minorTMHeight) { this.minorTMHeight = minorTMHeight; } public void setDisplayValueDistance(String displayValueDistance) { this.displayValueDistance = displayValueDistance; } public void setTickValueDistance(String tickValueDistance) { this.tickValueDistance = tickValueDistance; } public void setGaugeStartAngle(String gaugeStartAngle) { this.gaugeStartAngle = gaugeStartAngle; } public void setGaugeEndAngle(String gaugeEndAngle) { this.gaugeEndAngle = gaugeEndAngle; } public void setPalette(String palette) { this.palette = palette; } public void setShowValue(String showValue) { this.showValue = showValue; } public void setNumberSuffix(String numberSuffix) { this.numberSuffix = numberSuffix; } @Override public String toString() { return ToStringBuilder.reflectionToString(this); } }
上面那一堆的注解来自jaxb的jar包,下哪些包我就不说了。估计应该能看懂个所以然吧,那些属性都是这个gaugeChart所需要的图形显示属性,不必去细究,它需要什么,我们按上面添加就是了,不过貌似没有get()方法,不需要这个。里面注解的像这样的@XmlElement(name = "annotationGroup") ,这个annotationGroup也是像GaugeChart这样的java对象,需要被定义。
接下来我们可以在action中调用了。
private String getXxxXml() { String template = getTemplate(); String chartXml = JAXBUtils.process(template, GaugeChart.class, new JAXBUtils.ChartFiller<GaugeChart>() { public void fill(final GaugeChart chart) { Dial dial1 = new Dial(); dial1.setValue(""); dial1.setRearExtension("10"); chart.addDials(dial1); } }); return chartXml; }
这样的一个方法返回的就是一个xml的string型对象了,我们只需要在页面拿到这个string型的xml ,就可以通过fusioncharts(当然是我这里用了fusioncharts)来调用它并显示相应的图形了。
评论
3 楼
ron.luo
2014-12-31
干货,必须得顶。
2 楼
友友水
2013-06-08
。。。。不好意思,无心之失,删不掉前一条评论
1 楼
友友水
2013-06-08
[/flash][/flash][/flash][/flash][/flash][/flash][/flash][/flash][/flash][/flash][/flash][/flash][/flash][/flash][/flash][/flash][/flash][/flash][/flash]
发表评论
-
http简易工具类
2018-09-21 09:20 408http简易工具类, 方便http调用 -
微信JSSDK之添加微信卡券
2016-05-31 09:37 713微信卡券的使用,是在之前的微信jsapi基础上,再加上一次卡券 ... -
hibernate延迟加载 与 web应用 独立缓存架构的冲突
2013-04-26 11:03 1087延迟加载(Lazy Loading)是 ... -
jquery验证框架使用
2013-03-27 16:57 12811.使用jquery的表单验证框架,需要导入jquery的库文 ... -
结合webservice实现dwr推送
2012-08-22 13:26 5270情景: 客户端需要实时提醒服务,比如某个日程已过期的提醒, ... -
copy到粘贴板
2012-05-22 11:39 1141直接上代码,实现了IE和火狐下的copy content ... -
webService使用总结
2012-04-28 15:08 1218webService 是什么就不解释了,webservice有 ... -
小玩dwr实现服务器推送
2012-01-19 17:01 5548最近项目有需要用到‘推’,就是服务器端自动把消息推给客户端,就 ... -
jquery之事件error小提醒
2011-12-13 15:27 1371今天用到jquery的事件中的error方法,发现当标签img ... -
小想法
2011-10-17 16:48 0刚才有个想法,不知道是由什么联想而来,最近淘宝大战,看了些文章 ... -
原来这个如此容易 ---- 点击弹出确认框
2011-10-12 15:51 1271今天无意识的看到了点击按钮弹出确认框的 javascri ... -
groovy 小应用
2011-09-30 10:25 1135昨天使用groovy脚本生成大量的sql,实在是爽! 现 ... -
Jquery资料链接(供学习)
2011-08-08 17:16 658提供些资料链接,以备查找 http://jquery.com ... -
dwr使用小结
2011-08-05 15:32 1063很久没上来冒泡了 ,把dwr的使用经验搁上来,以后备用。 ... -
实践中filter的配置
2011-04-26 09:23 862配置一个权限filter,拦截一切没有登录却想访问只有登录后才 ... -
JSTL问题解决之-->报找不到C标签
2011-03-30 13:51 3031当jsp页面显示报找不到C:标签时的解决办法 在li ... -
jsp之自定义标签
2011-03-29 11:09 845http://javakeith.iteye.com/blog ... -
jsp之session对象
2011-03-29 11:06 835http://javakeith.iteye.com/blo ... -
jsp内置对象
2011-03-29 11:04 964感谢兄弟的总结 http://javakeith.iteye. ... -
JSTL自定义标签过程
2011-03-29 10:39 7271要定义自己的标签,首先写个java类,extends TagS ...
相关推荐
JAXB的使用JAXB的使用JAXB的使用JAXB的使用
如何更好地jaxb使用如何更好地jaxb使用如何更好地jaxb使用如何更好地jaxb使用
JAXB框架,作用是对象和XML之间相互转化,本文档包含代码示例,介绍对象和XML如果使用JAXB相互转化 在代码里面备注了具体使用的文字说明
JAXB使用Socket流进行双向通信源码
java -jar JAXB2_20060607.jar On Windows, you can just double-click the jar file to execute. Release Notes Browse the release notes online, including what's new. Technical Support Please subscribe to...
NULL 博文链接:https://dannyhz.iteye.com/blog/2316638
无需安装,解压后即可。jaxb-api.jar,jaxb-xjc.jar,jaxb-impl.jar,activation.jar等相关jar包在lib文件夹下。
本人在公司用到了jaxb技术,这是在公司的学习文档,今天跟高兴个大家分享。
如何使用JAXB框架定制Web服务行为.doc
最新的JAXB API手册! JAXB_2.2_API.chm
我刚写好的 一个简单的小程序 动行里面的read文件 是在idea 中用jaxb 读xml中数据
使用jaxb生成XML例子,含有例子和注解解析
jaxb-api jaxb-impl jar
JAXB的 eclipse插件 JAXB的 eclipse插件 JAXB的 eclipse插件 JAXB的 eclipse插件 JAXB的 eclipse插件
在使用webservice,mule esb等需要jaxb的项目里经常会出现 JAXB 2.0 API is being loaded from the bootstrap classloader这个错误,按照打出的信息Use the endorsed directory mechanism to place jaxb-api.jar in ...
JAXB的安装包及插件
使用jaxb根据xsd生成xml文档,不要积分的,赶快下载
JAXB注解相关技术JAXB注解相关技术JAXB注解相关技术JAXB注解相关技术JAXB注解相关技术JAXB注解相关技术
JAXB是jdk中集成的哟该来 xml和java 对象互相转换的工具,但是在android sdk中默认没有集成,需要自己添加相应的jar包