Announcement Announcement Module
Collapse
No announcement yet.
spring oxm castor unmarshalling incorrect results Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • spring oxm castor unmarshalling incorrect results

    hi there,
    I am using Spring castor integration classes provided in Spring OXM packages to unmarshal an xml string. But the problem is the unmarshalled object's each field contains the xml prolog of the xml response string in its contents..


    Here's my xmlmessage string
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <message schema_version="0.0.2">
      <request>
        <vehicle_type>PPV</vehicle_type>
      </request>
      <response>
        <vins/>
        <makes>
          <make>MAZDA</make>
        </makes>
        <models>
          <model>323 PROTEGE 4DR</model>
          <model>PROTEGE LX 4DR</model>
        </models>
        <years/>
        <vehicle_codes/>
      </response>
    </message>
    And here's my webservice MessageResponse object's fields
    Code:
    private ArrayList<String> vins;
    	 private ArrayList<String> makes;
    	 private ArrayList<String> models;
    	 private ArrayList<String> years;
    	 private ArrayList<String> vehicleCodes;
    . So after unmarshalling the xmlmessage string, when the "makes" arraylist is supposed to contain list of Strings
    Code:
    MAZDA, CHEVROLET
    It actually contains values like

    Code:
    <?xml version="1.0" encoding="UTF-8"?><make>MAZDA</make>,
    <?xml version="1.0" encoding="UTF-8"?><make>CHEVROLET</make>
    I am clueless , why its happening..

    Here's my castorMarshallerBean (P.S: I am not declaring my mappinglocations in this example in the following xml configuration because I setting it up programmatically in java code. But I have tried declaring the mappinglocation property as well with value
    classpath:mapping-file.xml but I got the same results.)

    Code:
    <bean id="marshaller" class="org.springframework.oxm.castor.CastorMarshaller">		 		 
    	</bean>

    Here's my mapping file
    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <!DOCTYPE mapping PUBLIC "-//EXOLAB/Castor Object Mapping DTD Version 1.0//EN" "http://castor.org/mapping.dtd">
    <mapping>	
    	<class name="com.abc.cl.clfleets.webservices.model.Message">
    		<map-to xml="message" />
    		<field name="Version" type="string" handler="com.abc.cl.util.castor.StringHandler">
    			<bind-xml name="schema_version" node="attribute"/>
    		</field>
    		<field name="Request" type="com.abc.cl.clfleets.webservices.model.MessageRequest">
    			<bind-xml name="request"  node="element"/>
    		</field>
    		<field name="Response" type="com.abc.cl.clfleets.webservices.model.MessageResponse" >
    			<bind-xml name="response" node="element" />
    		</field>
    		<field name="Errors" type="com.abc.cl.clfleets.webservices.model.VSearchError" >
    			<bind-xml name="errors" node="element" />
    		</field>			
    	</class>
    	<class name="com.abc.cl.clfleets.webservices.model.MessageRequest">
    		<map-to xml="request" />
    		<field name="Command" type="string" handler="com.abc.cl.util.castor.StringHandler">
    			<bind-xml name="command"/>
    		</field>
    		<field name="Vin" type="string" handler="com.abc.cl.util.castor.StringHandler">
    			<bind-xml name="vin"/>
    		</field>
    		<field name="Vehicle_type" type="string" handler="com.abc.cl.util.castor.StringHandler">
    			<bind-xml name="vehicle_type"/>
    		</field>
    		<field name="Make" type="string" handler="com.abc.cl.util.castor.StringHandler">
    			<bind-xml name="make"/>
    		</field>
    		<field name="Model" type="string" handler="com.abc.cl.util.castor.StringHandler">
    			<bind-xml name="model"/>
    		</field>
    		<field name="Year" type="string" handler="com.abc.cl.util.castor.StringHandler">
    			<bind-xml name="year"/>
    		</field>
    		<field name="Vehicle_code" type="string" handler="com.abc.cl.util.castor.StringHandler">
    			<bind-xml name="vehicle_code"/>
    		</field>
    	</class>
    	<class name="com.abc.cl.clfleets.webservices.model.MessageResponse">
    		<map-to xml="response" />
    		<field name="Vins" type="java.lang.String" collection="arraylist">
    			<bind-xml name="vin" location="vins" node="element"/>
    		</field>	
    		<field name="Makes" type="java.lang.String" collection="arraylist">
    			<bind-xml name="make" location="makes" node="element"/>
    		</field>	
    		<field name="Models" type="java.lang.String" collection="arraylist">
    			<bind-xml name="model" location="models" node="element"/>
    		</field>	
    		<field name="Years" type="java.lang.String" collection="arraylist">
    			<bind-xml name="year" location="years" node="element"/>
    		</field>	
    		<field name="VehicleCodes" type="java.lang.String" collection="arraylist">
    			<bind-xml name="vehicle_code" location="vehicle_codes" node="element"/>
    		</field>	
    	</class>
    	<class name="com.abc.cl.clfleets.webservices.model.VSearchError" >
    		<map-to xml="errors" />
    		<field name="ErrorId" type="string" handler="com.abc.cl.util.castor.StringHandler">
    			<bind-xml name="error_id"/>
    		</field>
    		<field name="ErrorDesc" type="string" handler="com.abc.cl.util.castor.StringHandler">
    			<bind-xml name="error_desc"/>
    		</field>
    	</class>
    </mapping>
    Here's my MessageResponse class (I have other Message, MessageRequest classes as well.. which don't seem to have any issues whatsoever..)

    Code:
    public class MessageResponse  {
    
    	 private ArrayList<String> vins;
    	 private ArrayList<String> makes;
    	 private ArrayList<String> models;
    	 private ArrayList<String> years;
    	 private ArrayList<String> vehicleCodes;
    	 
    	public MessageResponse() {
    		super();
    		vins = new ArrayList<String>();
    		makes = new ArrayList<String>();
    		models = new ArrayList<String>();
    		years = new ArrayList<String>();
    		vehicleCodes = new ArrayList<String>();
    	}
    
    	public ArrayList<String> getVins() {
    		return vins;
    	}
    
    	public void setVins(ArrayList<String> vins) {
    		this.vins = vins;
    	}
    
    	public ArrayList<String> getMakes() {
    		return makes;
    	}
    
    	public void setMakes(ArrayList<String> makes) {
    		this.makes = makes;
    	}
    
    	public ArrayList<String> getModels() {
    		return models;
    	}
    
    	public void setModels(ArrayList<String> models) {
    		this.models = models;
    	}
    
    	public ArrayList<String> getYears() {
    		return years;
    	}
    	
    	public void setYears(ArrayList<String> years) {
    		this.years = years;
    	}
    	
    	public ArrayList<String> getVehicleCodes() {
    		return vehicleCodes;
    	}
    
    	public void setVehicleCodes(ArrayList<String> vehicleCodes) {
    		this.vehicleCodes = vehicleCodes;
    	}
    
    	
    	
    
    }
    Here's my unmarshalling code ..
    Code:
    StringBufferInputStream is = new StringBufferInputStream(xmlResponse);
    marshaller.setMappingLocation(new ClassPathResource("vsearch-message-mapping.xml"));
    marshaller.setTargetClass(Message.class);
    Message message = (Message)marshaller.unmarshal(new StreamSource(is));
    Am i doing something wrong.. ?
    As I mentioned above, I have tried commenting out the code lines..
    Code:
    //marshaller.setMappingLocation(new ClassPathResource("vsearch-message-mapping.xml"));
    //marshaller.setTargetClass(Message.class);
    and setting these marshaller properties in the spring xml like this
    Code:
    <bean id="marshaller" class="org.springframework.oxm.castor.CastorMarshaller">
    		 <property name="mappingLocation" value="classpath:vsearch-message-mapping.xml" />
    		 
    	</bean>
    but I get the same results..
    Any help will be greatly appreciated..
Working...
X