0

I want to build a REST java backend (using jersey) for an existing JavaScript library (SmartClient). This JavaScript library sends requests in different format to my server.

How can I deserialize an XML to a container object, that contains different types of content?

Here is an example, with what I already tried:

import java.io.StringReader;

import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement
public class Box {

    static class Ball {
        public String size;
        public String toString() {return "Ball(" + size + ")";}
    }

    static class Cat {
        public String name;
        public String toString() {return "Cat(" + name + ")";}
    }

    public static void main(String[] args) throws Exception {
        printContent("<box><content><size>3</size></content></box>");
        printContent("<box><content><name>Mimi</name></content></box>");
    }

    private static void printContent(String xml) throws JAXBException {
        JAXBContext context = JAXBContext.newInstance(Box.class, Ball.class, Cat.class);
        Unmarshaller unmarshaller = context.createUnmarshaller();
        Box box = (Box) unmarshaller.unmarshal(new StringReader(xml));

        System.out.println(box.content);
    }

    // not working
    public Object content;

    // not working
    // @XmlMixed
    // public Object content;

    // not working
    // @XmlAnyElement
    // public List<Object> content;

    // not working
    // @XmlAnyElement(lax=true)
    // public List<Object> content;

    // not working
    // @XmlJavaTypeAdapter(ContentXmlAdapter.class)
    // public Object content;
    // static class ContentXmlAdapter extends XmlAdapter<String, Object>{
    // 
    //  @Override
    //  public Object unmarshal(String v) throws Exception {
    //      System.out.println("unmarshal("+v+")");
    //      return null;
    //  }
    // 
    //  public String marshal(Object v) throws Exception {throw new UnsupportedOperationException();}
    // }
}
slartidan
  • 20,403
  • 15
  • 83
  • 131
  • Why can't you create another class for Content and it's member can be `name` and `size`? – Pasupathi Rajamanickam May 06 '15 at 11:34
  • @Pasupathi would work for this simplified example, but would create lots of code for my real world structure. The other class for Content would have to summarize all fields of all possible contents - but I want to reuse separate, existing POJO classes. – slartidan May 06 '15 at 11:40
  • 1
    I tried a little but couldn't achieve. Map can be useful for this right? Like we can have key value pair as content.? Maybe this reference would help http://stackoverflow.com/questions/3941479/jaxb-how-to-marshall-map-into-keyvalue-key – Pasupathi Rajamanickam May 07 '15 at 03:48
  • @Pasupathi Yesterday, I had the same idea and even posted an answer there (lol). Sadly the "map" approach will only work for structures with one level of depth. Nested structures will not work with maps. I think I'll have to find another way. – slartidan May 07 '15 at 09:11

0 Answers0