users@jaxb.java.net

Re: unmarshalling ignores element-attribute 'xml'

From: Wolfgang Laun <wolfgang.laun_at_gmail.com>
Date: Wed, 18 Mar 2009 12:01:42 +0100

With an XML schema you could define

<?xml version="1.0" encoding="utf-8"?>
<xs:schema xmlns="http://foobar"
           xmlns:xs="http://www.w3.org/2001/XMLSchema"
           targetNamespace="http://foobar"
           elementFormDefault="qualified"
           version="2.0">

  <!-- import needed for xml:lang attribute -->
  <xs:import namespace="http://www.w3.org/XML/1998/namespace"
             schemaLocation="http://www.w3.org/2001/03/xml.xsd"/>

  <xs:element name="doc" type="DocType"/>
  <xs:complexType name="DocType">
    <xs:sequence>
      <xs:element name="Sentence" type="SentenceType"
          maxOccurs="unbounded"/>
    </xs:sequence>
  </xs:complexType>

  <xs:complexType name="SentenceType" mixed="true">
    <xs:sequence>
      <xs:element name="Sentence" type="SentenceType"/>
    </xs:sequence>
    <xs:attribute ref="xml:lang"/>
  </xs:complexType>

</xs:schema>

The generated classes let you marshal a document like this:
<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<doc xmlns="http://foobar">
  <Sentence xml:lang="en-GB">This is a sentence.</Sentence>
  <Sentence xml:lang="hu">Ez egy mondat.</Sentence>
</doc>

Unmarshalling in the usual way

    JAXBElement<?> jbo =
    (JAXBElement<?>)u.unmarshal( new File( "doc.xml" ) );
    DocType indoc = (DocType)jbo.getValue();
    for( SentenceType s: indoc.getSentence() ){
        System.out.println( s.getLang( ) );
        System.out.println( s.getContent( ) );
    }

prints

en-GB
[This is a sentence.]
hu
[Ez egy mondat.]

-W


On Tue, Mar 17, 2009 at 2:01 PM, korsdal <nabble_at_chicken-flu.de> wrote:

>
> hi,
>
> i just got stuck using jaxb2.1.9.
>
> while generating sources out of my DTD works fine,
> unmarshalling my xml-file does not work correctly.
>
> one of the xml-elements contains an attribute called 'lang' with 'xml' as
> its prefix:
>
> <myTag xml:lang="en-GB" ...>...</myTag>
>
>
> this attributes is always NULL in my JaxbObject after unmarshalling.
> debugging with specific ContentHandler, shows up, that
>
>
> public void startElement(String uri, String localName,
> String name, Attributes
> atts) throws SAXException{
>
> //....debugging attributes
> }
>
>
> contains the correct value of the attribute.
> so i guess, the unmarshaller simply ignores this attribute for any reasons.
> why?
>
> marshalling a whole, programmatically created Object-Tree considering this
> attribute works fine, though.
>
>
> any suggestions?
>
> thanks a lot
> tim
> --
> View this message in context:
> http://www.nabble.com/unmarshalling-ignores-element-attribute-%27xml%27-tp22558466p22558466.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
>
>