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)