users@jaxb.java.net

Re: JAXB Unmarshalling - missing object when only an attribute present

From: Wolfgang Laun <wolfgang.laun_at_gmail.com>
Date: Mon, 8 Dec 2008 19:47:30 +0100

I cannot reproduce this with JAXB 2.1 bundled with Java 1.6.
Below is my complete test. Please compare this to what you have,
perhaps I've overlooked something.

<?xml version="1.0" encoding="UTF-8"?>
<xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema">

<xs:complexType name="DocType">
  <xs:sequence>
    <xs:element name="begin" type="xs:string"/>
    <xs:element name="outer" type="OuterType"/>
    <xs:element name="end" type="xs:string"/>
  </xs:sequence>
</xs:complexType>

<xs:complexType name="OuterType">
  <xs:sequence>
    <xs:element name="inner1" type="xs:string" minOccurs="0"/>
    <xs:element name="inner2" type="xs:string" minOccurs="0"/>
  </xs:sequence>
  <xs:attribute name="outerAttribute" type="xs:string" use="optional"/>
</xs:complexType>

<xs:element name="doc" type="DocType"/>

</xs:schema>

== oa.xml =========

<doc>
 <begin>begin</begin>
 <outer outerAttribute="theOuterAttributeValue"/>
 <end>end</end>
</doc>

================

import java.io.*;
import javax.xml.bind.*;

import generated.*;

public class Main {

    static void unmarshal() throws Exception {
        JAXBContext jc = JAXBContext.newInstance( "generated" );
        Unmarshaller u = jc.createUnmarshaller();
        JAXBElement<DocType> jbe =
           (JAXBElement)u.unmarshal( new FileInputStream( "oa.xml" ) );
        DocType dt = jbe.getValue();
        OuterType ot = dt.getOuter();
        System.out.println( ot.getOuterAttribute() );
    }

    public static void main( String[] args ) throws Exception {
       unmarshal();
    }
}

Wolfgang



On Mon, Dec 8, 2008 at 5:02 PM, StickyLlama <scott.burnett_at_toyota-europe.com
> wrote:

>
> Hi,
>
> I'm JAXB 2.1 unmarshalling some xml which contains something like this
>
> <.....>
> <outerElement attributeName="XXXXXXXXXXXXXXXXXX">
> <innerElement1></innerElelment1>
> <innerElement2></innerElelment2>
> </outerElement>
> </....>
>
> outerElement is optional
> Both innerElement1 and innerElement2 are optional
> and attributeName is mandatory
>
> So in the final unmarshalled object - if either innerElement1 or
> innerElement2 are present - I get an outerElement object present
> If it is just the attributeName which is present - I get outerElement =
> null
>
> Therefore I lose the attributeName ?!
>
> How do I get an outerElement to be formed when only the attributeName is
> present ?
>
> Is this a binding setting in the xjb file ?
>
> Or is this a property I can set on the unmarshaller ?
>
> Thanks for your help
> --
> View this message in context:
> http://www.nabble.com/JAXB-Unmarshalling---missing-object-when-only-an-attribute-present-tp20898028p20898028.html
> Sent from the java.net - jaxb users mailing list archive at Nabble.com.
>
>
> ---------------------------------------------------------------------
> To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
> For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>
>