JAXB difference between @XmlSchemaType (name = "normalizedString") and @XmlJavaTypeAdapter (NormalizedStringAdapter.class)

I am writing a JAXB class without XSD compilation.

Can anyone tell me the difference between

@XmlSchemaType(name = "normalizedString")
private String normalized;

      

and

@XmlJavaTypeAdapter(NormalizedStringAdapter.class)
private String normalized;

      

?

+3


source to share


1 answer


@XmlSchemaType(name = "normalizedString")
private String normalized;

      

Using the specified annotation, the type xsd: normalizedString will be specified for the attribute / element corresponding to this property when the XML schema is generated.

@XmlJavaTypeAdapter(NormalizedStringAdapter.class)
private String normalized;

      

NormalizedStringAdapter

does the work during a non-marshal operation: strings, quotes, and tabs are removed.

Example

Input.xml

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <adapter>     A     B
C     </adapter>
    <schemaType>     A     B
C     </schemaType>
    <control>     A     B
C     </control>
</root>

      



Root

package forum383861;

import javax.xml.bind.annotation.*;
import javax.xml.bind.annotation.adapters.*;

@XmlRootElement
@XmlType(propOrder={"adapter", "schemaType", "control"})
public class Root {

    private String adapter;
    private String schemaType;
    private String control;

    @XmlJavaTypeAdapter(NormalizedStringAdapter.class)
    public String getAdapter() {
        return adapter;
    }

    public void setAdapter(String adpater) {
        this.adapter = adpater;
    }

    @XmlSchemaType(name="normalizedString")
    public String getSchemaType() {
        return schemaType;
    }

    public void setSchemaType(String schemaType) {
        this.schemaType = schemaType;
    }

    public String getControl() {
        return control;
    }

    public void setControl(String control) {
        this.control = control;
    }

}

      

Demo

package forum383861;

import java.io.*;
import javax.xml.bind.*;
import javax.xml.transform.Result;
import javax.xml.transform.stream.StreamResult;

public class Demo {

    public static void main(String[] args) throws Exception {
        JAXBContext jc = JAXBContext.newInstance(Root.class);

        Unmarshaller unmarshaller = jc.createUnmarshaller();
        File xml = new File("src/forum383861/input.xml");
        Root root = (Root) unmarshaller.unmarshal(xml);

        Marshaller marshaller = jc.createMarshaller();
        marshaller.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);
        marshaller.marshal(root, System.out);

        System.out.println();

        jc.generateSchema(new SchemaOutputResolver() {

            @Override
            public Result createOutput(String arg0, String arg1) throws IOException {
                StreamResult result = new StreamResult(System.out);
                result.setSystemId(arg1);
                return result;
            }

        });
    }

}

      

Output

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<root>
    <adapter>     A     B C     </adapter>
    <schemaType>     A     B
C     </schemaType>
    <control>     A     B
C     </control>
</root>

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<xs:schema version="1.0" xmlns:xs="http://www.w3.org/2001/XMLSchema">

  <xs:element name="root" type="root"/>

  <xs:complexType name="root">
    <xs:sequence>
      <xs:element name="adapter" type="xs:string" minOccurs="0"/>
      <xs:element name="schemaType" type="xs:normalizedString" minOccurs="0"/>
      <xs:element name="control" type="xs:string" minOccurs="0"/>
    </xs:sequence>
  </xs:complexType>
</xs:schema>

      

+6


source







All Articles