JAXB Object (not strongly typed) within class creating MarshalException

Discussion in 'Java' started by Greg, Apr 16, 2013.

  1. Greg

    Greg Guest

    I have a colleague that is trying to get JAXB to create XML and then createthe class back again where one of the member variables will just be an Object. That is, it will not be strongly typed and it will contain a myriad of different types depending on what the XML coming in contains. However, asimple example results in a MarshalException with linked exception: JAXBException: class (any class here) nor any of its super class is known to thiscontext. How can this be solved? An example of what is being tried is below. Thank you for your help!

    <code>
    package com.hp.asi.ui.hpicsm.rmi.impl;



    import java.io.StringWriter;

    import java.util.ArrayList;

    import java.util.HashSet;

    import java.util.List;

    import java.util.Set;



    import javax.xml.bind.JAXBContext;

    import javax.xml.bind.Marshaller;

    import javax.xml.bind.annotation.XmlAnyElement;

    import javax.xml.bind.annotation.XmlElement;

    import javax.xml.bind.annotation.XmlRootElement;

    import javax.xml.bind.annotation.XmlSeeAlso;



    public class testExample {

    public static void main(String[] args) {

    testExample ex = new testExample();



    ex.doIt();

    }



    public void doIt() {

    try {

    JAXBContext jaxbContext = JAXBContext.newInstance(testXML.class);

    Marshaller jaxbMarshaller = jaxbContext.createMarshaller();

    jaxbMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

    StringWriter writer = new StringWriter();



    Set<String> y = new HashSet<String>();

    y.add("hello");

    y.add("world");

    testXML x = new testXML(y);



    jaxbMarshaller.marshal(x, writer);



    System.out.println(writer.getBuffer().toString());



    } catch (Exception e) {

    System.out.println(e);

    }



    }



    @XmlRootElement

    public static class testXML {

    @XmlElement

    private Object testVal;



    public testXML(){



    }



    public testXML(Object y) {

    this.testVal = y;

    }

    }

    }
    </code>
    Greg, Apr 16, 2013
    #1
    1. Advertising

  2. On 16/04/2013 17:22, Greg allegedly wrote:
    > I have a colleague that is trying to get JAXB to create XML and then
    > create the class back again where one of the member variables will
    > just be an Object. That is, it will not be strongly typed and it
    > will contain a myriad of different types depending on what the XML
    > coming in contains. However, a simple example results in a
    > MarshalException with linked exception: JAXBException: class (any
    > class here) nor any of its super class is known to this context. How
    > can this be solved?


    <snip />

    I don't think it can be solved like this. How is the unmarshaller
    supposed to know what to unmarshal the data to?

    But more importantly, what's that data (the XML you created) gonna be
    used for? Some(one|thing) has to understand it, right? But if for
    some(one|thing) to understand it, it's oughta have a well-defined
    structure, ought it not? It's oughta have a schema, ought it not?

    Well, then bloody formalise and code that schema, and then if you want
    to have your JAXB context be aware of classes the use of which it cannot
    infer, use @XmlSeeAlso.

    --
    DF.
    Daniele Futtorovic, Apr 19, 2013
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Narayanan Sankaranarayanan

    Custom Namespace For Strongly Typed DataSet

    Narayanan Sankaranarayanan, Dec 15, 2004, in forum: ASP .Net
    Replies:
    0
    Views:
    1,078
    Narayanan Sankaranarayanan
    Dec 15, 2004
  2. =?Utf-8?B?QW5kcmUgUmFuaWVyaQ==?=

    Help with creating strongly typed DataSet

    =?Utf-8?B?QW5kcmUgUmFuaWVyaQ==?=, Apr 19, 2004, in forum: ASP .Net
    Replies:
    1
    Views:
    504
    Rick Strahl [MVP]
    Apr 19, 2004
  3. Replies:
    1
    Views:
    452
  4. Dot net work

    Bind strongly typed dictionary class to datagrid?

    Dot net work, Sep 7, 2004, in forum: ASP .Net Datagrid Control
    Replies:
    1
    Views:
    187
    Dot net work
    Sep 8, 2004
  5. Joe Rattz

    Strongly typed vars not working

    Joe Rattz, Oct 30, 2003, in forum: ASP .Net Web Services
    Replies:
    0
    Views:
    91
    Joe Rattz
    Oct 30, 2003
Loading...

Share This Page