JAXB使用经验总结

使用JAXB首先要知道它是干什么的 
   当然,切入正题 
      很多时候我们需要把认知世界转化为我们熟悉的java对象,以供方便操作。这里,JAXB可以把xml对象转化为我们的java对象,也可以把java对象转化为xml对象。这时候我们就得知道它的两个转化方法。 
      一个是unmarshal(),一个是marshal() 

unmarshal()是把xml对象转化为我们需要的java对象的方法,自然marshal()是把java对象转化为xml对象的一个过程。 

   我们需要的估计就是这两个方法的精华,只需要用到这么多就可以完成很多的事情了。下面看代码: 

   

Java代码   收藏代码
  1. private static <T> T unmarshal(File templateFile, JAXBContext context) throws JAXBException {  
  2.   
  3.        final Unmarshaller unmarshaller = context.createUnmarshaller();  
  4.   
  5.        // Unmarshal the XML in the stringWriter back into an object  
  6.        final T chart = (T) unmarshaller.unmarshal(templateFile);  
  7.   
  8.        return chart;  
  9.    }  
  10.   
  11.    @SuppressWarnings("unchecked")  
  12.    private static <T> T unmarshal(String template, JAXBContext context) throws JAXBException {  
  13.   
  14.        final Unmarshaller unmarshaller = context.createUnmarshaller();  
  15.   
  16.        // Unmarshal the XML in the stringWriter back into an object  
  17.        final T chart = (T) unmarshaller.unmarshal(new StringReader(template));  
  18.   
  19.        return chart;  
  20.    }  
  21.   
  22.      




这里templateFile 和 template 都是xml文件对象,这样的两个overload method就可以完成由xml对象转化为java对象了。 

Java代码   收藏代码
  1. private static <T> String marshal(JAXBContext context, final T chart) throws JAXBException {  
  2.         // Create the marshaller, this is the nifty little thing that will  
  3.         // actually transform the object into XML  
  4.         final Marshaller marshaller = context.createMarshaller();  
  5.   
  6.         // Create a stringWriter to hold the XML  
  7.         final StringWriter stringWriter = new StringWriter();  
  8.   
  9.         // Marshal the javaObject and write the XML to the stringWriter  
  10.         marshaller.marshal(chart, stringWriter);  
  11.   
  12.         String chartXml = stringWriter.toString();  
  13.         return chartXml;  
  14.     }  



marshal()方法也贡献上。

方法都具备了 可以开工了。 

Java代码   收藏代码
  1.   public static <T> String process(File templateFile, Class<T> type, ChartFiller<T> filler) {  
  2.        String chartXml = null;  
  3.   
  4.        try {  
  5.            JAXBContext context = JAXBContext.newInstance(type);  
  6.   
  7.            final T chart = (T) unmarshal(templateFile, context);  
  8.   
  9.            filler.fill(chart);  
  10.   
  11.            chartXml = marshal(context, chart);  
  12.   
  13.        } catch (JAXBException e) {  
  14.            log.error(e.getMessage(), e);  
  15.        }  
  16.        return chartXml;  
  17.    }  
  18.   
  19.    public static <T> String process(String template, Class<T> type, ChartFiller<T> filler) {  
  20.        String chartXml = null;  
  21.   
  22.        try {  
  23.            JAXBContext context = JAXBContext.newInstance(type);  
  24.   
  25.            final T chart = (T) unmarshal(template, context);  
  26.   
  27.            filler.fill(chart);  
  28.   
  29.            chartXml = marshal(context, chart);  
  30.   
  31.        } catch (JAXBException e) {  
  32.            log.error(e.getMessage(), e);  
  33.        }  
  34.        return chartXml;  
  35.    }  
  36.   
  37. public interface ChartFiller<T> {  
  38.        void fill(T chart);  
  39.    }  



工具方法都上齐了,你就可以在你需要的地方调用了。当然,我还没讲我们的java操作对象。 

下面举个例子,比如上面出现的chart 就是我们需要操纵的对象,它可能是一个xml对象,而我们需要操作的是java对象,晕了吧: 
   比如我们的chart是在页面需要显示的图形,比如fusioncharts 的某个图,先定义为chart,它会是一个接口类,是所有xxxChart的父类。 比如我的具体chart名叫GaugeChart。 

代码如下,别说晕说郁闷了: 

Java代码   收藏代码
  1. public interface Chart {  
  2.   
  3. }  




Java代码   收藏代码
  1. @XmlRootElement(name = "chart")  
  2. public class GaugeChart implements Chart {  
  3.   
  4.     @XmlAttribute  
  5.     private String lowerLimit;  
  6.   
  7.     @XmlAttribute  
  8.     private String upperLimit;  
  9.   
  10.     @XmlAttribute  
  11.     private String lowerLimitDisplay;  
  12.   
  13.     @XmlAttribute  
  14.     private String upperLimitDisplay;  
  15.   
  16.     @XmlAttribute  
  17.     private String majorTMNumber;  
  18.   
  19.     @XmlAttribute  
  20.     private String majorTMColor;  
  21.   
  22.     @XmlAttribute  
  23.     private String majorTMHeight;  
  24.   
  25.     @XmlAttribute  
  26.     private String minorTMNumber;  
  27.   
  28.     @XmlAttribute  
  29.     private String minorTMColor;  
  30.   
  31.     @XmlAttribute  
  32.     private String minorTMHeight;  
  33.   
  34.     @XmlAttribute  
  35.     private String displayValueDistance;  
  36.   
  37.     @XmlAttribute  
  38.     private String tickValueDistance;  
  39.   
  40.     @XmlAttribute  
  41.     private String gaugeStartAngle;  
  42.   
  43.     @XmlAttribute  
  44.     private String gaugeEndAngle;  
  45.   
  46.     @XmlAttribute  
  47.     private String palette;  
  48.   
  49.     @XmlAttribute  
  50.     private String showValue;  
  51.   
  52.     @XmlAttribute  
  53.     private String numberSuffix;  
  54.   
  55.     @XmlElementWrapper(name = "colorRange")  
  56.     @XmlElement(name = "color")  
  57.     private List<Color> colors;  
  58.   
  59.     @XmlElementWrapper(name = "dials")  
  60.     @XmlElement(name = "dial")  
  61.     private List<Dial> dials;  
  62.   
  63.     @XmlElementWrapper(name = "annotations")  
  64.     @XmlElement(name = "annotationGroup")  
  65.     private List<AnnotationGroup> annotations;  
  66.   
  67.     public void addAnnotationGroup(AnnotationGroup annotationGroup) {  
  68.         if (annotations == null)  
  69.             annotations = new LinkedList<AnnotationGroup>();  
  70.         this.annotations.add(annotationGroup);  
  71.     }  
  72.   
  73.     public void addDials(Dial dial) {  
  74.         if (dials == null)  
  75.             dials = new LinkedList<Dial>();  
  76.         this.dials.add(dial);  
  77.     }  
  78.   
  79.     public void setAnnotations(List<AnnotationGroup> annotations) {  
  80.         this.annotations = annotations;  
  81.     }  
  82.   
  83.     public void setDials(List<Dial> dials) {  
  84.         this.dials = dials;  
  85.     }  
  86.   
  87.     public void setColors(List<Color> colors) {  
  88.         this.colors = colors;  
  89.     }  
  90.   
  91.     public void addColor(Color color) {  
  92.         if (color == null)  
  93.             colors = new LinkedList<Color>();  
  94.         this.colors.add(color);  
  95.     }  
  96.   
  97.     public void setLowerLimit(String lowerLimit) {  
  98.         this.lowerLimit = lowerLimit;  
  99.     }  
  100.   
  101.     public void setUpperLimit(String upperLimit) {  
  102.         this.upperLimit = upperLimit;  
  103.     }  
  104.   
  105.     public void setLowerLimitDisplay(String lowerLimitDisplay) {  
  106.         this.lowerLimitDisplay = lowerLimitDisplay;  
  107.     }  
  108.   
  109.     public void setUpperLimitDisplay(String upperLimitDisplay) {  
  110.         this.upperLimitDisplay = upperLimitDisplay;  
  111.     }  
  112.   
  113.     public void setMajorTMNumber(String majorTMNumber) {  
  114.         this.majorTMNumber = majorTMNumber;  
  115.     }  
  116.   
  117.     public void setMajorTMColor(String majorTMColor) {  
  118.         this.majorTMColor = majorTMColor;  
  119.     }  
  120.   
  121.     public void setMajorTMHeight(String majorTMHeight) {  
  122.         this.majorTMHeight = majorTMHeight;  
  123.     }  
  124.   
  125.     public void setMinorTMNumber(String minorTMNumber) {  
  126.         this.minorTMNumber = minorTMNumber;  
  127.     }  
  128.   
  129.     public void setMinorTMColor(String minorTMColor) {  
  130.         this.minorTMColor = minorTMColor;  
  131.     }  
  132.   
  133.     public void setMinorTMHeight(String minorTMHeight) {  
  134.         this.minorTMHeight = minorTMHeight;  
  135.     }  
  136.   
  137.     public void setDisplayValueDistance(String displayValueDistance) {  
  138.         this.displayValueDistance = displayValueDistance;  
  139.     }  
  140.   
  141.     public void setTickValueDistance(String tickValueDistance) {  
  142.         this.tickValueDistance = tickValueDistance;  
  143.     }  
  144.   
  145.     public void setGaugeStartAngle(String gaugeStartAngle) {  
  146.         this.gaugeStartAngle = gaugeStartAngle;  
  147.     }  
  148.   
  149.     public void setGaugeEndAngle(String gaugeEndAngle) {  
  150.         this.gaugeEndAngle = gaugeEndAngle;  
  151.     }  
  152.   
  153.     public void setPalette(String palette) {  
  154.         this.palette = palette;  
  155.     }  
  156.   
  157.     public void setShowValue(String showValue) {  
  158.         this.showValue = showValue;  
  159.     }  
  160.   
  161.     public void setNumberSuffix(String numberSuffix) {  
  162.         this.numberSuffix = numberSuffix;  
  163.     }  
  164.   
  165.     @Override  
  166.     public String toString() {  
  167.         return ToStringBuilder.reflectionToString(this);  
  168.     }  
  169. }  



  上面那一堆的注解来自jaxb的jar包,下哪些包我就不说了。估计应该能看懂个所以然吧,那些属性都是这个gaugeChart所需要的图形显示属性,不必去细究,它需要什么,我们按上面添加就是了,不过貌似没有get()方法,不需要这个。里面注解的像这样的@XmlElement(name = "annotationGroup") ,这个annotationGroup也是像GaugeChart这样的java对象,需要被定义。 


接下来我们可以在action中调用了。 
  
  

Java代码   收藏代码
  1. private String getXxxXml() {  
  2.   
  3.        String template = getTemplate();  
  4.        String chartXml = JAXBUtils.process(template, GaugeChart.class,  
  5.                new JAXBUtils.ChartFiller<GaugeChart>() {  
  6.   
  7.                    public void fill(final GaugeChart chart) {  
  8.                        Dial dial1 = new Dial();  
  9.                        dial1.setValue("");  
  10.                        dial1.setRearExtension("10");  
  11.                        chart.addDials(dial1);  
  12.                    }  
  13.                });  
  14.        return chartXml;  
  15.    }  
  16.   
  17.     


这样的一个方法返回的就是一个xml的string型对象了,我们只需要在页面拿到这个string型的xml ,就可以通过fusioncharts(当然是我这里用了fusioncharts)来调用它并显示相应的图形了。 

猜你喜欢

转载自suliangyi.iteye.com/blog/1837117