users@jaxb.java.net

RE: [jaxb2] Namespace and other questions

From: Brennan Spies <brennanspies_at_hotmail.com>
Date: Tue, 3 Apr 2007 23:01:14 -0700

 

Yes, the classes are done by hand (not xjc) and no, there is no subclassing
of other JAXB-annotated classes (both WebsphereConnectionConfig and
JSR160ConnectionConfig do implement an interface ConnectionConfig, though).

 

 

 

Some code (fragments):

 

 

@XmlRootElement(name="websphere", namespace="http://www.ejgallo.com/jmx")

@XmlAccessorType(XmlAccessType.NONE)

@XmlType(name="websphereConnectionConfig",

            propOrder={"host","port","protocol","sslConfig"})

 

public class WebsphereConnectionConfig implements ConnectionConfig

{

      @XmlTransient private String name;

      @XmlElement private String host;

      @XmlElement private String port;

      @XmlElement

      private String protocol;

      @XmlAttribute private boolean securityEnabled;

      @XmlAttribute private boolean authenticationRequired;

      @XmlElement(name="ssl")

        private SslConfig sslConfig;
 
=====================================================================
 

@XmlRootElement(name="remote-api", namespace="http://www.ejgallo.com/jmx")

@XmlAccessorType(XmlAccessType.NONE)

@XmlType(name="jsr160ConnectionConfig",

 
propOrder={"host","port","protocol","urlPath","sslConfig","environment"})

public class JSR160ConnectionConfig implements ConnectionConfig

{

      @XmlTransient private String name;

      @XmlElement private String host;

      @XmlElement private String port;

      @XmlElement private String protocol;

      @XmlElement(name="url") private String urlPath;

      @XmlAttribute private boolean securityEnabled;

      @XmlAttribute private boolean authenticationRequired;

      @XmlElement(name="ssl") private SslConfig sslConfig;

      @XmlElement private HashMap<String,Object> environment = new
HashMap<String,Object>();
 
========================================================================
Part of the JUnit test:

      

private void doJaxbMarshall(ConnectionConfig config) throws Exception

      {

            JAXBContext ctx = JAXBContext.newInstance(config.getClass());

            Element confElem =
existingDoc.createElementNS("http://www.ejgallo.com/jmx", "connection"););

            confElem.setAttribute("name", config.getName());

            existingDoc.getDocumentElement().appendChild(confElem);

            Marshaller marshaller = ctx.createMarshaller();

 
marshaller.setProperty("com.sun.xml.bind.namespacePrefixMapper",new
NamespacePrefixMapperImpl());

            marshaller.marshal(config, confElem);

            printDocument(existingDoc);

        }
 
 
========================================================================

      private ConnectionConfig doJaxbUnmarshall(Class configClass, String
configName) throws Exception {

            JAXBContext ctx = JAXBContext.newInstance(configClass);

            Node node = getFromTree(configName);

            if(node==null)

                  throw new IllegalStateException("Node from XPath is
null");

            Unmarshaller unmarshaller = ctx.createUnmarshaller();

            ConnectionConfig config =
(ConnectionConfig)unmarshaller.unmarshal(node);

            config.setName(configName);

            return config;

        }
 
 
===========================================================================
 
Adding a namespace="." to all the @XmlElement seems to work, but seems
unnecessarily verbose.
 
 
Thanks,
 
Brennan
 
 
Date: Wed, 4 Apr 2007 11:01:07 +1000
From: Dmitri Colebatch <dim_at_colebatch.com>
Content-Type: text/plain; charset=WINDOWS-1252; format=flowed
Subject: [jaxb2] Namespace and other questions
 
 
Hi Brennan,
 
> 1) XmlAccessType.PROPERTY or XmlAccessType.PUBLIC_MEMBER give me
errors
> complaining of duplicates.the runtime would complain that I had duplicates
> such as "location" and "getLocation()". Why? I had to switch to
> XmlAccessType.NONE and annotate each field member individually.
 
Are you generating these classes using xjc, or have you written them
by hand? Do you have any subclassing? My guess is that you've
written them by hand and subclassed another class that also has JAXB
annotations. Could you post some code?
 
> 2) When marshalling to <websphere> or <remote-api> elements, JAXB
would
> set the default namespace to null and then set that element to the
namespace
> I had declared in the annotation, like
>
> <ns4:websphere xmlns="" xmlns:ns4="mynamespace">
>
> .even though the default namespace for all of the DOM is "mynamespace".
> Playing around with "##default" didn't help.
>
> 3) The behavior in #2 caused all of the child elements of <websphere>
> or <remote-api> to have null namespaces. I had to manually set the
> 'namespace' attribute on the individual @XmlElement annotations in order
to
> set the namespace correctly. Is there a way to simply do this at the class
> level?
 
You can do it with package level annotations, I'm not aware of a way
to do it at the class level (but would be happy to hear of any).
 
cheers,
dim