Gurnard
Gurnard

Reputation: 1775

jaxb set string type

How can I get JAXB to marshall a string or primitive data type and set the type="string", or type="int" in the resulting XML. I have tried specifying a specific type for each field

@XmlSchemaType(name = "string",namespace = "http://www.w3.org/2001/XMLSchema",type = String.class)

But this makes no difference the resulting XML is without type.

Thanks for reading

Update this is basically what I have in a JaxB class:

@XmlElement(required = true)
protected Keys keys;
protected String workflowID;
    protected String fromFigure;
    protected String fromPort;

This is the resulting XML

        <keys type="draw2d.ArrayList">
            <data type="Array">
                <element type="draw2d.FlowConnectionModel">
                    <workflowID>d8f71b92-dc69-4115-9095-d748265d4e68</workflowID>
                    <fromFigure>706531d9-cd03-4347-9ba2-d9b525035e0d</fromFigure>
                    <fromPort>out_right_initialState</fromPort>

Notice that there is a type set for the keys, data and element types but none for the primitive data types of workflowID, fromFigure and fromPort. What I want is this:

        <keys type="draw2d.ArrayList">
            <data type="Array">
                <element type="draw2d.FlowConnectionModel">
                    <workflowID type="string">d8f71b92-dc69-4115-9095-d748265d4e68</workflowID>
                    <fromFigure type="string">706531d9-cd03-4347-9ba2-d9b525035e0d</fromFigure>
                    <fromPort type="string">out_right_initialState</fromPort>

Upvotes: 0

Views: 818

Answers (2)

Gurnard
Gurnard

Reputation: 1775

In the end I had to change the schema to use complex types as opposed to simple types. A snippet from the original schema

     <xs:element name="layoutWorkflowID" type="xs:string" minOccurs="1" maxOccurs="1"/>
    <xs:element name="layoutInstanceID" type="xs:string" minOccurs="1" maxOccurs="1"/>

Changed to this

    <xs:element ref="layoutWorkflowID" minOccurs="1" maxOccurs="1"/>
    <xs:element ref="layoutInstanceID" minOccurs="1" maxOccurs="1"/>

With extra complex types

  <xs:element name="layoutWorkflowID">
<xs:complexType>
  <xs:simpleContent>
    <xs:extension base="xs:NCName">
      <xs:attribute name="type" use="required" type="xs:NCName"/>
    </xs:extension>
  </xs:simpleContent>
</xs:complexType>

And now I get what I need when marshalling with every type specified.

<layoutWorkflowID type="string">WorkflowHTML</layoutWorkflowID>
<layoutInstanceID type="string">6</layoutInstanceID>

Thanks for those that responded and read.

Upvotes: 0

AlexR
AlexR

Reputation: 115328

You will not see any difference in produced XML. You are expected to see difference in XSD if you generate it from your class.

Upvotes: 1

Related Questions