# JAXB: Marshalling



## phiLue (12. Jan 2010)

Guten Abend 

Ich habe folgendes Problem:
Ich soll aus einer Java-Klasse eine XML Datei erstellen, die Java-Klasse sieht wie folgt aus


```
package de.bdr.ws.types;


/**
 * Abstraktes Objekt für Auftragsdaten und Auftragsresultate in der
 * Datenaufbereitung und Produktion; (wird nicht direkt instanziiert)
 */
public class Message  implements java.io.Serializable {
    /* eindeutige Id des Auftrages */
    private org.apache.axis.types.Token MESOrderId;

    /* MES Arbeitsgangnummer (4 stellig alphanum) */
    private org.apache.axis.types.Token MESOperationId;

    /* MES Folgennummer (2 stellig alphanum) */
    private org.apache.axis.types.Token MESOrderSequence;

    /* MES Maschinenkennung (8 stellig alphanum) */
    private org.apache.axis.types.Token MESResourceId;

    /* MES Produkt/Materialkennung (18 stellig char) */
    private org.apache.axis.types.Token MESMaterialNumber;

    /* eindeutige Id der Requestes/Message (6 stellig) */
    private org.apache.axis.types.Token messageId;

    /* Erzeugungszeitpunkt des Requestes/Message */
    private java.util.Calendar messageTimestamp;

    public Message() {
    }

    public Message(
           org.apache.axis.types.Token MESOrderId,
           org.apache.axis.types.Token MESOperationId,
           org.apache.axis.types.Token MESOrderSequence,
           org.apache.axis.types.Token MESResourceId,
           org.apache.axis.types.Token MESMaterialNumber,
           org.apache.axis.types.Token messageId,
           java.util.Calendar messageTimestamp) {
           this.MESOrderId = MESOrderId;
           this.MESOperationId = MESOperationId;
           this.MESOrderSequence = MESOrderSequence;
           this.MESResourceId = MESResourceId;
           this.MESMaterialNumber = MESMaterialNumber;
           this.messageId = messageId;
           this.messageTimestamp = messageTimestamp;
    }


    /**
     * Gets the MESOrderId value for this Message.
     * 
     * @return MESOrderId   * eindeutige Id des Auftrages
     */
    public org.apache.axis.types.Token getMESOrderId() {
        return MESOrderId;
    }


    /**
     * Sets the MESOrderId value for this Message.
     * 
     * @param MESOrderId   * eindeutige Id des Auftrages
     */
    public void setMESOrderId(org.apache.axis.types.Token MESOrderId) {
        this.MESOrderId = MESOrderId;
    }


    /**
     * Gets the MESOperationId value for this Message.
     * 
     * @return MESOperationId   * MES Arbeitsgangnummer (4 stellig alphanum)
     */
    public org.apache.axis.types.Token getMESOperationId() {
        return MESOperationId;
    }


    /**
     * Sets the MESOperationId value for this Message.
     * 
     * @param MESOperationId   * MES Arbeitsgangnummer (4 stellig alphanum)
     */
    public void setMESOperationId(org.apache.axis.types.Token MESOperationId) {
        this.MESOperationId = MESOperationId;
    }


    /**
     * Gets the MESOrderSequence value for this Message.
     * 
     * @return MESOrderSequence   * MES Folgennummer (2 stellig alphanum)
     */
    public org.apache.axis.types.Token getMESOrderSequence() {
        return MESOrderSequence;
    }


    /**
     * Sets the MESOrderSequence value for this Message.
     * 
     * @param MESOrderSequence   * MES Folgennummer (2 stellig alphanum)
     */
    public void setMESOrderSequence(org.apache.axis.types.Token MESOrderSequence) {
        this.MESOrderSequence = MESOrderSequence;
    }


    /**
     * Gets the MESResourceId value for this Message.
     * 
     * @return MESResourceId   * MES Maschinenkennung (8 stellig alphanum)
     */
    public org.apache.axis.types.Token getMESResourceId() {
        return MESResourceId;
    }


    /**
     * Sets the MESResourceId value for this Message.
     * 
     * @param MESResourceId   * MES Maschinenkennung (8 stellig alphanum)
     */
    public void setMESResourceId(org.apache.axis.types.Token MESResourceId) {
        this.MESResourceId = MESResourceId;
    }


    /**
     * Gets the MESMaterialNumber value for this Message.
     * 
     * @return MESMaterialNumber   * MES Produkt/Materialkennung (18 stellig char)
     */
    public org.apache.axis.types.Token getMESMaterialNumber() {
        return MESMaterialNumber;
    }


    /**
     * Sets the MESMaterialNumber value for this Message.
     * 
     * @param MESMaterialNumber   * MES Produkt/Materialkennung (18 stellig char)
     */
    public void setMESMaterialNumber(org.apache.axis.types.Token MESMaterialNumber) {
        this.MESMaterialNumber = MESMaterialNumber;
    }


    /**
     * Gets the messageId value for this Message.
     * 
     * @return messageId   * eindeutige Id der Requestes/Message (6 stellig)
     */
    public org.apache.axis.types.Token getMessageId() {
        return messageId;
    }


    /**
     * Sets the messageId value for this Message.
     * 
     * @param messageId   * eindeutige Id der Requestes/Message (6 stellig)
     */
    public void setMessageId(org.apache.axis.types.Token messageId) {
        this.messageId = messageId;
    }


    /**
     * Gets the messageTimestamp value for this Message.
     * 
     * @return messageTimestamp   * Erzeugungszeitpunkt des Requestes/Message
     */
    public java.util.Calendar getMessageTimestamp() {
        return messageTimestamp;
    }


    /**
     * Sets the messageTimestamp value for this Message.
     * 
     * @param messageTimestamp   * Erzeugungszeitpunkt des Requestes/Message
     */
    public void setMessageTimestamp(java.util.Calendar messageTimestamp) {
        this.messageTimestamp = messageTimestamp;
    }

    private java.lang.Object __equalsCalc = null;
    public synchronized boolean equals(java.lang.Object obj) {
        if (!(obj instanceof Message)) return false;
        Message other = (Message) obj;
        if (obj == null) return false;
        if (this == obj) return true;
        if (__equalsCalc != null) {
            return (__equalsCalc == obj);
        }
        __equalsCalc = obj;
        boolean _equals;
        _equals = true && 
            ((this.MESOrderId==null && other.getMESOrderId()==null) || 
             (this.MESOrderId!=null &&
              this.MESOrderId.equals(other.getMESOrderId()))) &&
            ((this.MESOperationId==null && other.getMESOperationId()==null) || 
             (this.MESOperationId!=null &&
              this.MESOperationId.equals(other.getMESOperationId()))) &&
            ((this.MESOrderSequence==null && other.getMESOrderSequence()==null) || 
             (this.MESOrderSequence!=null &&
              this.MESOrderSequence.equals(other.getMESOrderSequence()))) &&
            ((this.MESResourceId==null && other.getMESResourceId()==null) || 
             (this.MESResourceId!=null &&
              this.MESResourceId.equals(other.getMESResourceId()))) &&
            ((this.MESMaterialNumber==null && other.getMESMaterialNumber()==null) || 
             (this.MESMaterialNumber!=null &&
              this.MESMaterialNumber.equals(other.getMESMaterialNumber()))) &&
            ((this.messageId==null && other.getMessageId()==null) || 
             (this.messageId!=null &&
              this.messageId.equals(other.getMessageId()))) &&
            ((this.messageTimestamp==null && other.getMessageTimestamp()==null) || 
             (this.messageTimestamp!=null &&
              this.messageTimestamp.equals(other.getMessageTimestamp())));
        __equalsCalc = null;
        return _equals;
    }

    private boolean __hashCodeCalc = false;
    public synchronized int hashCode() {
        if (__hashCodeCalc) {
            return 0;
        }
        __hashCodeCalc = true;
        int _hashCode = 1;
        if (getMESOrderId() != null) {
            _hashCode += getMESOrderId().hashCode();
        }
        if (getMESOperationId() != null) {
            _hashCode += getMESOperationId().hashCode();
        }
        if (getMESOrderSequence() != null) {
            _hashCode += getMESOrderSequence().hashCode();
        }
        if (getMESResourceId() != null) {
            _hashCode += getMESResourceId().hashCode();
        }
        if (getMESMaterialNumber() != null) {
            _hashCode += getMESMaterialNumber().hashCode();
        }
        if (getMessageId() != null) {
            _hashCode += getMessageId().hashCode();
        }
        if (getMessageTimestamp() != null) {
            _hashCode += getMessageTimestamp().hashCode();
        }
        __hashCodeCalc = false;
        return _hashCode;
    }

    // Type metadata
    private static org.apache.axis.description.TypeDesc typeDesc =
        new org.apache.axis.description.TypeDesc(Message.class, true);

    static {
        typeDesc.setXmlType(new javax.xml.namespace.QName("http://types.ws.bdr.de", "Message"));
        org.apache.axis.description.ElementDesc elemField = new org.apache.axis.description.ElementDesc();
        elemField.setFieldName("MESOrderId");
        elemField.setXmlName(new javax.xml.namespace.QName("http://types.ws.bdr.de", "MESOrderId"));
        elemField.setXmlType(new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "token"));
        elemField.setNillable(false);
        typeDesc.addFieldDesc(elemField);
        elemField = new org.apache.axis.description.ElementDesc();
        elemField.setFieldName("MESOperationId");
        elemField.setXmlName(new javax.xml.namespace.QName("http://types.ws.bdr.de", "MESOperationId"));
        elemField.setXmlType(new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "token"));
        elemField.setNillable(false);
        typeDesc.addFieldDesc(elemField);
        elemField = new org.apache.axis.description.ElementDesc();
        elemField.setFieldName("MESOrderSequence");
        elemField.setXmlName(new javax.xml.namespace.QName("http://types.ws.bdr.de", "MESOrderSequence"));
        elemField.setXmlType(new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "token"));
        elemField.setNillable(false);
        typeDesc.addFieldDesc(elemField);
        elemField = new org.apache.axis.description.ElementDesc();
        elemField.setFieldName("MESResourceId");
        elemField.setXmlName(new javax.xml.namespace.QName("http://types.ws.bdr.de", "MESResourceId"));
        elemField.setXmlType(new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "token"));
        elemField.setNillable(false);
        typeDesc.addFieldDesc(elemField);
        elemField = new org.apache.axis.description.ElementDesc();
        elemField.setFieldName("MESMaterialNumber");
        elemField.setXmlName(new javax.xml.namespace.QName("http://types.ws.bdr.de", "MESMaterialNumber"));
        elemField.setXmlType(new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "token"));
        elemField.setNillable(false);
        typeDesc.addFieldDesc(elemField);
        elemField = new org.apache.axis.description.ElementDesc();
        elemField.setFieldName("messageId");
        elemField.setXmlName(new javax.xml.namespace.QName("http://types.ws.bdr.de", "MessageId"));
        elemField.setXmlType(new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "token"));
        elemField.setNillable(false);
        typeDesc.addFieldDesc(elemField);
        elemField = new org.apache.axis.description.ElementDesc();
        elemField.setFieldName("messageTimestamp");
        elemField.setXmlName(new javax.xml.namespace.QName("http://types.ws.bdr.de", "MessageTimestamp"));
        elemField.setXmlType(new javax.xml.namespace.QName("http://www.w3.org/2001/XMLSchema", "dateTime"));
        elemField.setNillable(false);
        typeDesc.addFieldDesc(elemField);
    }

    /**
     * Return type metadata object
     */
    public static org.apache.axis.description.TypeDesc getTypeDesc() {
        return typeDesc;
    }

    /**
     * Get Custom Serializer
     */
    public static org.apache.axis.encoding.Serializer getSerializer(
           java.lang.String mechType, 
           java.lang.Class _javaType,  
           javax.xml.namespace.QName _xmlType) {
        return 
          new  org.apache.axis.encoding.ser.BeanSerializer(
            _javaType, _xmlType, typeDesc);
    }

    /**
     * Get Custom Deserializer
     */
    public static org.apache.axis.encoding.Deserializer getDeserializer(
           java.lang.String mechType, 
           java.lang.Class _javaType,  
           javax.xml.namespace.QName _xmlType) {
        return 
          new  org.apache.axis.encoding.ser.BeanDeserializer(
            _javaType, _xmlType, typeDesc);
    }

}
```

Das Problem ist nun das er einfache Datentypen darstellt, auch den Calendar usw. allerdings die Tokens nicht. Diese Felder bleiben im XML-Dokument welches ich erstelle immer leer. Wie bekomm ich das hin, das er mir auch die Tokens darstellt? Hoffe mir kann geholfen werden q____q

Grüße & Dank
phiLue


----------



## Noctarius (12. Jan 2010)

Reicht es nicht, wenn du diese Token als String speicherst?


----------



## phiLue (13. Jan 2010)

Leider funktioniert das nicht weil ich eine XML Datei von einem anderen Programm geschickt bekomme welches ich nicht ändern kann und wo diese als Tokens definiert sind. Wenn ich dies Versuche bekomm ich beim Versuch folgende Fehlermeldung:


```
SCHWERWIEGEND: Could not convert org.apache.axis.types.Token to bean field 'MESOrderId', type java.lang.String
13.01.2010 10:30:33 org.apache.axis.transport.http.AxisServlet processAxisFault
INFO: AxisFault:
AxisFault
 faultCode: {http://schemas.xmlsoap.org/soap/envelope/}Server.userException
 faultSubcode: 
 faultString: java.lang.IllegalArgumentException: argument type mismatch
 faultActor: 
 faultNode: 
 faultDetail: 
	{http://xml.apache.org/axis/}stackTrace:java.lang.IllegalArgumentException: argument type mismatch
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.apache.axis.utils.BeanPropertyDescriptor.set(BeanPropertyDescriptor.java:142)
	at org.apache.axis.encoding.ser.BeanPropertyTarget.set(BeanPropertyTarget.java:75)
	at org.apache.axis.encoding.DeserializerImpl.valueComplete(DeserializerImpl.java:249)
	at org.apache.axis.encoding.DeserializerImpl.endElement(DeserializerImpl.java:509)
	at org.apache.axis.encoding.DeserializationContext.endElement(DeserializationContext.java:1087)
	at org.apache.axis.message.SAX2EventRecorder.replay(SAX2EventRecorder.java:171)
	at org.apache.axis.message.MessageElement.publishToHandler(MessageElement.java:1141)
	at org.apache.axis.message.RPCElement.deserialize(RPCElement.java:236)
	at org.apache.axis.message.RPCElement.getParams(RPCElement.java:384)
	at org.apache.axis.providers.java.RPCProvider.processMessage(RPCProvider.java:81)
	at org.apache.axis.providers.java.JavaProvider.invoke(JavaProvider.java:323)
	at org.apache.axis.strategies.InvocationStrategy.visit(InvocationStrategy.java:32)
	at org.apache.axis.SimpleChain.doVisiting(SimpleChain.java:118)
	at org.apache.axis.SimpleChain.invoke(SimpleChain.java:83)
	at org.apache.axis.handlers.soap.SOAPService.invoke(SOAPService.java:454)
	at org.apache.axis.server.AxisServer.invoke(AxisServer.java:281)
	at org.apache.axis.transport.http.AxisServlet.doPost(AxisServlet.java:699)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
	at org.apache.axis.transport.http.AxisServletBase.service(AxisServletBase.java:327)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
	at java.lang.Thread.run(Unknown Source)

	{http://xml.apache.org/axis/}hostname:WORKSTATION
	{http://xml.apache.org/axis/}isRuntimeException:true

java.lang.IllegalArgumentException: argument type mismatch
	at org.apache.axis.AxisFault.makeFault(AxisFault.java:101)
	at org.apache.axis.providers.java.JavaProvider.invoke(JavaProvider.java:333)
	at org.apache.axis.strategies.InvocationStrategy.visit(InvocationStrategy.java:32)
	at org.apache.axis.SimpleChain.doVisiting(SimpleChain.java:118)
	at org.apache.axis.SimpleChain.invoke(SimpleChain.java:83)
	at org.apache.axis.handlers.soap.SOAPService.invoke(SOAPService.java:454)
	at org.apache.axis.server.AxisServer.invoke(AxisServer.java:281)
	at org.apache.axis.transport.http.AxisServlet.doPost(AxisServlet.java:699)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:710)
	at org.apache.axis.transport.http.AxisServletBase.service(AxisServletBase.java:327)
	at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
	at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
	at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
	at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:233)
	at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
	at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
	at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
	at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
	at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:263)
	at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
	at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:584)
	at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
	at java.lang.Thread.run(Unknown Source)
Caused by: java.lang.IllegalArgumentException: argument type mismatch
	at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
	at sun.reflect.NativeMethodAccessorImpl.invoke(Unknown Source)
	at sun.reflect.DelegatingMethodAccessorImpl.invoke(Unknown Source)
	at java.lang.reflect.Method.invoke(Unknown Source)
	at org.apache.axis.utils.BeanPropertyDescriptor.set(BeanPropertyDescriptor.java:142)
	at org.apache.axis.encoding.ser.BeanPropertyTarget.set(BeanPropertyTarget.java:75)
	at org.apache.axis.encoding.DeserializerImpl.valueComplete(DeserializerImpl.java:249)
	at org.apache.axis.encoding.DeserializerImpl.endElement(DeserializerImpl.java:509)
	at org.apache.axis.encoding.DeserializationContext.endElement(DeserializationContext.java:1087)
	at org.apache.axis.message.SAX2EventRecorder.replay(SAX2EventRecorder.java:171)
	at org.apache.axis.message.MessageElement.publishToHandler(MessageElement.java:1141)
	at org.apache.axis.message.RPCElement.deserialize(RPCElement.java:236)
	at org.apache.axis.message.RPCElement.getParams(RPCElement.java:384)
	at org.apache.axis.providers.java.RPCProvider.processMessage(RPCProvider.java:81)
	at org.apache.axis.providers.java.JavaProvider.invoke(JavaProvider.java:323)
	... 21 more
```


----------



## Noctarius (13. Jan 2010)

Ja aber du kannst doch einen eigenen Property-Marshaller und -Unmarshaller schreiben welcher die Konvertierung von Token nach String und umgekehrt vornimmt


----------



## phiLue (13. Jan 2010)

Das klingt super 
Kannst du mir dafür vllt ein kurzes Tutorial oder ne Seite geben oder vllt auch nur einen Anstoß wo ich nachlesen kann wie das funktioniert? :>
Wäre sehr nett, das klingt genau nach dem was ich brauche :'D

Danke schonmal


----------



## Noctarius (13. Jan 2010)

```
@XmlType
@XmlAccessorType(XmlAccessType.FIELD)
public class Message implements java.io.Serializable {
	private static final long serialVersionUID = 1L;

	/* eindeutige Id des Auftrages */
	@XmlElement
	@XmlJavaTypeAdapter(TokenAdapter.class)
	private org.apache.axis.types.Token MESOrderId;

	/* MES Arbeitsgangnummer (4 stellig alphanum) */
	@XmlElement
	@XmlJavaTypeAdapter(TokenAdapter.class)
	private org.apache.axis.types.Token MESOperationId;

	/* MES Folgennummer (2 stellig alphanum) */
	@XmlElement
	@XmlJavaTypeAdapter(TokenAdapter.class)
	private org.apache.axis.types.Token MESOrderSequence;

	/* MES Maschinenkennung (8 stellig alphanum) */
	@XmlElement
	@XmlJavaTypeAdapter(TokenAdapter.class)
	private org.apache.axis.types.Token MESResourceId;

	/* MES Produkt/Materialkennung (18 stellig char) */
	@XmlElement
	@XmlJavaTypeAdapter(TokenAdapter.class)
	private org.apache.axis.types.Token MESMaterialNumber;

	/* eindeutige Id der Requestes/Message (6 stellig) */
	@XmlElement
	@XmlJavaTypeAdapter(TokenAdapter.class)
	private org.apache.axis.types.Token messageId;

	/* Erzeugungszeitpunkt des Requestes/Message */
	@XmlElement
	private java.util.Calendar messageTimestamp;
	
	//Constructors
	//Getter & Setter
}
```


```
public class TokenAdapter extends XmlAdapter<String, Token> {

	@Override
	public String marshal(Token v) throws Exception {
		return v.toString();
	}

	@Override
	public Token unmarshal(String v) throws Exception {
		Token token = new Token();
		token.setValue(v);
		return token; 
	}

}
```

Genauere Infos findest durch die Suche nach XmlJavaTypeAdapter bei Google.


----------



## phiLue (15. Jan 2010)

Vielen Dank, genau sowas hab ich gebraucht. Hab es auch mitlerweile hinbekommen, vielen Dank nochmal.


----------



## Noctarius (15. Jan 2010)

kein Problem  Einfach Danke anklicken *gg*


----------

