users@jaxb.java.net

Re: JAXB marshals DOM Element with/without attributes in xs:any differently

From: Nasly <naslyy_at_gmail.com>
Date: Sun, 3 Jun 2007 16:16:15 -0700 (PDT)

Hi Shailender,

I'm also having a similar problem. More precisely I need to get rid of empty
namespace that JAXB adds whenever there's attribute present in xs:any
element (why JAXB put xmlns="" ? element "data" in your output(2) is not
bound to any namespace, which should have been bound to
http://www.example.com/2007/message namespace) . This empty namespace causes
clients to fail in unmarshalling.

Did you manage to resolve the issue? Is there a way to inform JAXB not to
put empty namespace?

Thanks,
Nasly


Shailender Bathula wrote:
>
> Hi
>
> I have searched through "Enterprise Technologies - Java Technology & XML"
> forum [http://forum.java.sun.com/forum.jspa?forumID=34] using keywords I
> could think of, but have not been able to see a thread discussing this
> problem.
>
> Can you please provide me some keywords that I can use to find the thread
> in the forum?
>
> Thanks
> Shailender
>
> Kohsuke Kawaguchi <Kohsuke.Kawaguchi_at_Sun.COM> wrote on 14/03/2007 05:21:03
> a.m.:
>
>>
>> I remember seeing this somewhere. Did we talk about this in the forum?
>>
>> Shailender Bathula wrote:
>> > Hi all
>> >
>> > JAXB marshals a DOM Element with attributes in xs:any differently to a
> DOM
>> > Element without attributes. That is, while marshalling a DOM Element
> with
>> > attributes JAXB starts using namespace prefixes on elements. Following
> is
>> > the sample output, schemas and code. The project is also attached in
> JAXB
>> > RI sample format.
>> >
>> > Is it possible to use JAXB Marshaller to generate Output (1) (i.e.
> elements
>> > of xs:any without namespace prefixes etc.) when using a DOM Element
> with
>> > attributes in xs:any (i.e. data2.xsd)?
>> >
>> > I have run this test using JAXB RI 2.1.2 (JAXB2_20070125.jar)
>> >
>> > Thanks
>> > Shailender
>> >
>> > Output (1): message marshalled with a DOM Element without attributes
> (i.e.
>> > data1.xsd)
>> >
>> > <message xsi:schemaLocation="http://www.example.com/2007/message
>> > message.xsd"
>> > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>> > xmlns="http://www.example.com/2007/message">
>> > <header>
>> > <version>1</version>
>> > </header>
>> > <payload>
>> > <data
> xsi:schemaLocation="http://www.example.com/2007/message
>> > data1.xsd">
>> > <version>1</version>
>> > <line>hello</line>
>> > </data>
>> > </payload>
>> > </message>
>> >
>> > -----------------------------------------------------
>> >
>> > Output (2): message marshalled with a DOM Element with attributes (i.e.
>> > data2.xsd)
>> >
>> > <message xsi:schemaLocation="http://www.example.com/2007/message
>> > message.xsd"
>> > xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
>> > xmlns="http://www.example.com/2007/message">
>> > <header>
>> > <version>1</version>
>> > </header>
>> > <payload>
>> > <data version="1"
>> > xsi:schemaLocation="http://www.example.com/2007/message data2.xsd"
>> > xmlns:ns3="http://www.example.com/2007/message"
>> > xmlns="">
>> > <ns3:line
>> > xmlns="http://www.example.com/2007/message">hello</ns3:line>
>> > </data>
>> > </payload>
>> > </message>
>> >
>> > -----------------------------------------------------
>> >
>> > message.xsd -- wrapper to encapsulate xs:any
>> >
>> > <?xml version="1.0" encoding="UTF-8"?>
>> > <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
>> > xmlns="http://www.example.com/2007/message"
>> > targetNamespace="http://www.example.com/2007/message"
>> > elementFormDefault="qualified">
>> > <xs:element name="message" type="message_type"/>
>> > <xs:complexType name="message_type">
>> > <xs:sequence>
>> > <xs:element name="header"
> type="message_header_type"/>
>> > <xs:element name="payload"
> type="message_payload_type"/>
>> > </xs:sequence>
>> > </xs:complexType>
>> > <xs:complexType name="message_header_type">
>> > <xs:sequence>
>> > <xs:element name="version" type="xs:int" />
>> > </xs:sequence>
>> > </xs:complexType>
>> > <xs:complexType name="message_payload_type">
>> > <xs:sequence>
>> > <xs:any processContents="lax" minOccurs="0"/>
>> > </xs:sequence>
>> > </xs:complexType>
>> > </xs:schema>
>> >
>> > -----------------------------------------------------
>> >
>> > data1.xsd -- content of xs:any
>> >
>> > <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
>> > xmlns="http://www.example.com/2007/message"
>> > targetNamespace="http://www.example.com/2007/message"
>> > elementFormDefault="qualified">
>> > <xs:element name="data" type="data_type" />
>> > <xs:complexType name="data_type">
>> > <xs:sequence>
>> > <xs:element name="version" type="xs:int"/>
>> > <xs:element name="line" type="xs:string"/>
>> > </xs:sequence>
>> > </xs:complexType>
>> > </xs:schema>
>> >
>> > -----------------------------------------------------
>> >
>> > data2.xsd -- content of xs:any
>> >
>> > <xs:schema xmlns:xs="http://www.w3.org/2001/XMLSchema"
>> > xmlns="http://www.example.com/2007/message"
>> > targetNamespace="http://www.example.com/2007/message"
>> > elementFormDefault="qualified">
>> > <xs:element name="data" type="data_type" />
>> > <xs:complexType name="data_type">
>> > <xs:sequence>
>> > <xs:element name="line" type="xs:string"/>
>> > </xs:sequence>
>> > <xs:attribute name="version" type="xs:int" use="required"/>
>> > </xs:complexType>
>> > </xs:schema>
>> >
>> > -----------------------------------------------------
>> >
>> > // Main.java
>> > // message.xsd has been compiled into "message" package
>> > // data1.xsd has been compiled into "data1" package
>> > // data2.xsd has been compiled into "data2" package
>> >
>> > import data1.*;
>> > import data2.*;
>> > import message.*;
>> > import java.io.*;
>> > import javax.xml.*;
>> > import javax.xml.bind.*;
>> > import javax.xml.parsers.*;
>> > import javax.xml.validation.*;
>> > import org.w3c.dom.*;
>> >
>> > public class Main {
>> > public static void main(String args[]) throws Exception {
>> > printMessage(buildDataElement1());
>> > printMessage(buildDataElement2());
>> > }
>> >
>> > private static void printMessage(org.w3c.dom.Element any) throws
>> > Exception {
>> > JAXBContext messageCtx = JAXBContext.newInstance("message");
>> > message.ObjectFactory messageOF = new message.ObjectFactory();
>> >
>> > message.MessageHeaderType headerType = new
>> > message.MessageHeaderType();
>> > headerType.setVersion(1);
>> >
>> > message.MessagePayloadType payloadType =
>> > new message.MessagePayloadType();
>> > payloadType.setAny(any);
>> >
>> > message.MessageType messageType = new message.MessageType();
>> > messageType.setHeader(headerType);
>> > messageType.setPayload(payloadType);
>> > JAXBElement<message.MessageType> messageElement =
>> > messageOF.createMessage(messageType);
>> >
>> > StringWriter sw = new StringWriter();
>> > SchemaFactory sf = SchemaFactory.newInstance(
>> > XMLConstants.W3C_XML_SCHEMA_NS_URI);
>> > Schema messageSchema = sf.newSchema(new File("message.xsd"));
>> > Marshaller messageMarshaller = messageCtx.createMarshaller();
>> > messageMarshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION,
>> > "http://www.example.com/2007/message
> message.xsd");
>> > messageMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
>> > Boolean.TRUE);
>> > messageMarshaller.setSchema(messageSchema);
>> > messageMarshaller.marshal(messageElement, sw);
>> > System.out.println(sw.toString());
>> > }
>> >
>> > private static org.w3c.dom.Element buildDataElement1() throws
> Exception
>> > {
>> > JAXBContext dataCtx1 = JAXBContext.newInstance("data1");
>> > data1.ObjectFactory dataOF = new data1.ObjectFactory();
>> >
>> > data1.DataType dataType = new data1.DataType();
>> > dataType.setVersion(1);
>> > dataType.setLine("hello");
>> > JAXBElement<data1.DataType> dataElement1 =
>> > dataOF.createData(dataType);
>> >
>> > SchemaFactory sf = SchemaFactory.newInstance(
>> > XMLConstants.W3C_XML_SCHEMA_NS_URI);
>> > Schema dataSchema = sf.newSchema(new File("data1.xsd"));
>> > Marshaller dataMarshaller = dataCtx1.createMarshaller();
>> > DocumentBuilderFactory dbf =
> DocumentBuilderFactory.newInstance();
>> > dbf.setNamespaceAware(true);
>> > DocumentBuilder db = dbf.newDocumentBuilder();
>> > Document doc = db.newDocument();
>> > dataMarshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION,
>> > "http://www.example.com/2007/message
> data1.xsd");
>> > dataMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
>> > Boolean.TRUE);
>> > dataMarshaller.setSchema(dataSchema);
>> > dataMarshaller.marshal(dataElement1, doc);
>> > return doc.getDocumentElement();
>> > }
>> >
>> > private static org.w3c.dom.Element buildDataElement2() throws
> Exception
>> > {
>> > JAXBContext dataCtx1 = JAXBContext.newInstance("data2");
>> > data2.ObjectFactory dataOF = new data2.ObjectFactory();
>> >
>> > data2.DataType dataType = new data2.DataType();
>> > dataType.setVersion(1);
>> > dataType.setLine("hello");
>> > JAXBElement<data2.DataType> dataElement1 =
>> > dataOF.createData(dataType);
>> >
>> > SchemaFactory sf = SchemaFactory.newInstance(
>> > XMLConstants.W3C_XML_SCHEMA_NS_URI);
>> > Schema dataSchema = sf.newSchema(new File("data2.xsd"));
>> > Marshaller dataMarshaller = dataCtx1.createMarshaller();
>> > DocumentBuilderFactory dbf =
> DocumentBuilderFactory.newInstance();
>> > dbf.setNamespaceAware(true);
>> > DocumentBuilder db = dbf.newDocumentBuilder();
>> > Document doc = db.newDocument();
>> > dataMarshaller.setProperty(Marshaller.JAXB_SCHEMA_LOCATION,
>> > "http://www.example.com/2007/message
> data2.xsd");
>> > dataMarshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT,
>> > Boolean.TRUE);
>> > dataMarshaller.setSchema(dataSchema);
>> > dataMarshaller.marshal(dataElement1, doc);
>> > return doc.getDocumentElement();
>> > }
>> > }
>> >
>> > -----------------------------------------------------
>> >
>> > (See attached file: any-dom-marshal.jar)
>> >
>> >
>> >
> ------------------------------------------------------------------------
>> >
>> > ---------------------------------------------------------------------
>> > To unsubscribe, e-mail: users-unsubscribe_at_jaxb.dev.java.net
>> > For additional commands, e-mail: users-help_at_jaxb.dev.java.net
>>
>>
>> --
>> Kohsuke Kawaguchi
>> Sun Microsystems kohsuke.kawaguchi_at_sun.com
>
> ---------------------------------------------------------------------
> 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/JAXB-marshals-DOM-Element-with-without-attributes-in-xs%3Aany-differently-tf3331702.html#a10941811
Sent from the java.net - jaxb users mailing list archive at Nabble.com.