I'm having troubles converting from int[] in Java to a String in XML. My data
XML looks like this:



<?xml version="1.0" encoding="UTF-8"?>
<data-object>
    <ints>3 1 4 1 5 </ints>
</data-object>



and I have an IntObject.java:
public class IntObject {
        private int [] ints;

        public int[] getInts() {
                return ints;
        }

        public void setInts(int[] ints) {
                this.ints = ints;
        }
}



I'm trying to map this with the following mapping 


<mapping>
<class name="castortest.IntObject">
<map-to xml="data-object" />
    <field name="Ints" collection="array" type="java.lang.String"
handler="castortest.IntConvert">
            <bind-xml name="ints" node="element"/>
    </field>
</class>
</mapping>


and the following IntConvert.java:



import java.util.Enumeration;
import org.exolab.castor.mapping.GeneralizedFieldHandler;
public class IntConvert extends GeneralizedFieldHandler
{
        public IntConvert() {
                super();
                setCollectionIteration(false);
        }

        /**
         * From int[] to String
         * 
        public Object convertUponGet(Object value) {
                StringBuffer keyString = new StringBuffer();
                for (Enumeration<Integer> ae = (Enumeration<Integer>) value;
ae.hasMoreElements() ;) {
                        keyString.append(ae.nextElement()+" ");
                }
                return keyString.toString();
        }


        /** 
         * From String to int []
         * 
        public Object convertUponSet(Object value) {
                if (value == null) return null;
                
                String [] keyArray = ((String) value).split(" ");
                int [] intArray = new int[keyArray.length];
                for (int i = 0; i < intArray.length; i++) {
                        intArray[i] = Integer.parseInt(keyArray[i]);
                }
                
                return intArray;
        }

        public Class<?> getFieldType() {
                System.out.println("getFieldType");
                int test [] = new int [] {3};
                return test.getClass();
        }

        public Object newInstance( Object parent )
        throws IllegalStateException
        {
                System.out.println("hmm");
                //-- Since it's marked as a string...just return null,
                //-- it's not needed.
                return null;
        }
}




Marshalling works, but I get an error when Unmarshalling:
org.exolab.castor.xml.MarshalException: Type conversion error: could not set
value of [EMAIL PROTECTED] with value of type
java.lang.String{File: [not available]; line: 3; column: 28}
        at
org.exolab.castor.xml.Unmarshaller.convertSAXExceptionToMarshalException(Unmarshaller.java:761)
        at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:727)
        at org.exolab.castor.xml.Unmarshaller.unmarshal(Unmarshaller.java:616)
        at castortest.UnmarshallerTest.main(UnmarshallerTest.java:29)
Caused by: java.lang.IllegalArgumentException: Type conversion error: could
not set value of [EMAIL PROTECTED] with value of type
java.lang.String
        at
org.exolab.castor.mapping.loader.FieldHandlerImpl.setValue(FieldHandlerImpl.java:512)
        at
org.exolab.castor.xml.UnmarshalHandler.endElement(UnmarshalHandler.java:1068)
        at
org.exolab.castor.xml.UnmarshalHandler.endElement(UnmarshalHandler.java:1159)
        at
com.sun.org.apache.xerces.internal.parsers.AbstractSAXParser.endElement(AbstractSAXParser.java:601)
(longer stack trace down)




Note that I'm using setCollectionIteration(false); because I want my XML to
only contain one     <ints>3 1 4 1 5 </ints> line, not
<ints>3</ints><ints>1</ints> etc.

It does work when I change the last line of IntConvert to

                return intArray[0];       // only returning first item
        }

but then I only get back one number; it doesn't add all numbers.

Any suggestions?
-- 
View this message in context: 
http://www.nabble.com/Problems-converting-int---to-String-tp19953000p19953000.html
Sent from the Castor - User mailing list archive at Nabble.com.


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email


Reply via email to