users@jaxb.java.net

Re: marshalling and unmarhalling a sequence of any element

From: Davide Gesino <williamkidd_at_libero.it>
Date: Fri, 28 Sep 2007 07:41:37 -0700 (PDT)

Oh sorry, at last I understood...
I was able to recover all, thanks a lot for your precious help Aleksei.

For the sake of completeness here is the unmarshalling code.

JAXBElement<AnyCollectionLax> tsk =
(JAXBElement<AnyCollectionLax>)smarsha.unmarshal(new File("marshal.xml"));
AnyCollectionLax lax = tsk.getValue();
JAXBElement<StringCollection> colla =
(JAXBElement<StringCollection>)lax.getAny().get(0);
StringCollection collect = colla.getValue();
for(String strrr: collect.getMyString()){
System.out.println(strrr);
}







Davide Gesino wrote:
>
> Maybe I am not using well JAXB api.
> I have slightly changed your xsd, transforming a global complex type to a
> list of strings.
> Here it is.
>
> <xsd:schema targetNamespace="http://gesinity.it/beans"
> xmlns:test="http://gesinity.it/beans"
> xmlns:xsd="http://www.w3.org/2001/XMLSchema"
> elementFormDefault="qualified">
>
> <xsd:element name="anyCollectionLax" type="test:AnyCollectionLax" />
> <xsd:element name="stringCollection" type="test:StringCollection" />
>
> <xsd:complexType name="AnyCollectionLax">
> <xsd:sequence>
> <xsd:any namespace="##any" processContents="lax"
> minOccurs="0" maxOccurs="unbounded" />
> </xsd:sequence>
> </xsd:complexType>
> <xsd:complexType name="StringCollection">
> <xsd:sequence>
> <xsd:element name="myString" type="xsd:string" minOccurs="0"
> maxOccurs="unbounded">
> </xsd:element>
> </xsd:sequence>
> </xsd:complexType>
> </xsd:schema>
>
>
> then I Marshall an instance in this way:
>
>
>
> JAXBContext jaxbContext=JAXBContext.newInstance("it.gesinity.beans");
> Marshaller marshaller=jaxbContext.createMarshaller();
> ObjectFactory fattori = new ObjectFactory();
> AnyCollectionLax lax = fattori.createAnyCollectionLax();
> StringCollection str = fattori.createStringCollection();
> str.getMyString().add("GOD");
> str.getMyString().add("SAVES");
> str.getMyString().add("THE");
> str.getMyString().add("QUEEN");
> lax.getAny().add(str);
> marshaller.marshal(lax, new FileOutputStream(new File("marshal.xml")));
>
>
> and I produce the following file.
>
>
> <?xml version="1.0" encoding="UTF-8" standalone="yes"?>
> <anyCollectionLax xmlns="http://gesinity.it/beans">
> <stringCollection>
> <myString>GOD</myString>
> <myString>SAVES</myString>
> <myString>THE</myString>
> <myString>QUEEN</myString>
> </stringCollection>
> </anyCollectionLax>
>
>
> the I try to unmarshall the file:
>
>
> JAXBContext jaxbContext=JAXBContext.newInstance("it.gesinity.beans");
> Unmarshaller smarsha = jaxbContext.createUnmarshaller();
> Object tsk = smarsha.unmarshal(new File("marshalxml"));
>
>
> Now I would expect tsk were an instance of AnyCollectionLax, but it is
> not... it is an instance of JAXBElement (and the only instance of
> StringCollection is contained inside).
> Am I going in the right direction?
> Is it the right approach?
> How restore back the AnyCollectionLax instance, the StringCollection and
> all my beloved Strings?
>
> Thanks again.
>
>
>
>
>
>
>
>
>
>
>
> Aleksei Valikov wrote:
>>
>> Hi.
>>
>>> At first I wanted to put into the any a complex type, then I decided to
>>> switch to a String.
>>> So there is not any way to recover the type of the objects inside the
>>> <any>?
>>> I mean... it is a structural problem of a bug that will be fixed in
>>> future
>>> releases?
>>
>> Maybe I did not express myself clear enough.
>> <any> or generic content elements is represented in JAXB by the
>> JAXBElement.
>>
>> JAXBElement contains:
>> * Element name (QName).
>> * Declared type.
>> * Scope (whether it's a global element or a local definition).
>> * Value - an unmarshalled object.
>> * Whether it's nillled or not.
>>
>> So. There IS a way to recover the type of the object inside the <any>.
>> Take a look at the declared type of simply use the value - it IS the
>> typed unmarshalled object.
>>
>> Maybe I'm not understanding your difficulties, would you post the code
>> you experience problems with?
>>
>>> I'll try chainging the schema as you suggested, using a global element
>>> inside the<any>.
>>> Can you attach the URL of one of your previous discussions on the
>>> argument?
>>> I could not find them.
>>> Thanx for the help.
>>
>> http://www.nabble.com/How-can-I-unmarshall-a-xsd%3Aany-element---tf4067146.html
>>
>> Bye.
>> /lexi
>>
>> ---------------------------------------------------------------------
>> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
>> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>>
>>
>>
>
>

-- 
View this message in context: http://www.nabble.com/marshalling-and-unmarhalling-a-sequence-of-any-element-tf4533411.html#a12941845
Sent from the java.net - jaxb users mailing list archive at Nabble.com.