Quantcast
Channel: web – Imran Tariq's Blog
Viewing all articles
Browse latest Browse all 33

Map attributes in xstream

$
0
0

Suppose you have XML like this

  1. <code><qualification name="access">
  2.     <attribute name="abc">OK</attribute>
  3.     <requesturi>http://stackoverflow.com</requesturi>
  4. </qualification>

Class is:

  1. public class Qualification {
  2.   @XStreamAsAttribute
  3.   private String name;
  4.  
  5.   private String requesturi;
  6. }

Now we’ll map this line attributes here: OK

—————–

We have to write converter.

  1. package com.xstream;
  2.  
  3. import com.thoughtworks.xstream.annotations.XStreamAsAttribute;
  4.  
  5. public class Attribute {
  6.  
  7.   @XStreamAsAttribute
  8.   private String name;
  9.  
  10.   private String value;
  11.  
  12.   public Attribute(String name, String value) {
  13.     this.name = name;
  14.     this.value = value;
  15.   }
  16.  
  17.   /**
  18.    * @return the name
  19.    */
  20.   public String getName() {
  21.     return name;
  22.   }
  23.  
  24.   /**
  25.    * @param name the name to set
  26.    */
  27.   public void setName(String name) {
  28.     this.name = name;
  29.   }
  30.  
  31.   /**
  32.    * @return the value
  33.    */
  34.   public String getValue() {
  35.     return value;
  36.   }
  37.  
  38.   /**
  39.    * @param value the value to set
  40.    */
  41.   public void setValue(String value) {
  42.     this.value = value;
  43.   }
  44.  
  45.   /* (non&minus;Javadoc)
  46.    * @see java.lang.Object#toString()
  47.    */
  48.   @Override
  49.   public String toString() {
  50.     return "Attribute [name=" + name + ", value=" + value + "]";
  51.   }
  52.  
  53. }

AttributeConverter class is:

  1. package com.xstream;
  2.  
  3. import com.thoughtworks.xstream.converters.Converter;
  4. import com.thoughtworks.xstream.converters.MarshallingContext;
  5. import com.thoughtworks.xstream.converters.UnmarshallingContext;
  6. import com.thoughtworks.xstream.io.HierarchicalStreamReader;
  7. import com.thoughtworks.xstream.io.HierarchicalStreamWriter;
  8.  
  9. public class AttributeConverter implements Converter{
  10.  
  11.   @Override
  12.   public boolean canConvert(Class clazz) {
  13.     return clazz.equals(Attribute.class);
  14.   }
  15.  
  16.   @Override
  17.   public void marshal(Object object, HierarchicalStreamWriter writer, MarshallingContext context) {
  18.     System.out.println("object = " + object.toString());
  19.     Attribute attribute = (Attribute) object;
  20.     writer.addAttribute("name", attribute.getName());  
  21.     writer.setValue(attribute.getValue());  
  22.  
  23.   }
  24.  
  25.   @Override
  26.   public Object unmarshal(HierarchicalStreamReader reader, UnmarshallingContext context) {
  27.     return new Attribute(reader.getAttribute("name"), reader.getValue());
  28.  
  29.   }
  30.  
  31. }

Use this in Qualification class as:

  1. @XStreamConverter(AttributeConverter.class)
  2.   private Attribute attribute;

Register converter in main class as:

  1. xstream.registerConverter(new AttributeConverter());

Share


Viewing all articles
Browse latest Browse all 33

Trending Articles