Announcement Announcement Module
Collapse
No announcement yet.
get attribute using XStreamMarshaller Page Title Module
Move Remove Collapse
X
Conversation Detail Module
Collapse
  • Filter
  • Time
  • Show
Clear All
new posts

  • get attribute using XStreamMarshaller

    Hi,

    I have step in spring batch that read xml, process and write, but is a problem to get attributes from xml for elements everything work fine.

    I use:
    springVersion: 3.0.1.RELEASE
    springBatchVersion: 2.1.0.RELEASE
    spring-oxm: 1.5.9
    spring-oxm-tiger: 1.5.9
    spring-oxm: 3.0.1.RELEASE

    my xml where problem is get "num" attribute:

    Code:
    <?xml version="1.0" encoding="UTF-8"?>
    <records>
    	<trade num="1296">
    		<isin>XYZ0001</isin>
    		<quantity>5</quantity>
    		<price>11.39</price>
    		<customer>Customer1</customer>
    	</trade>
    	<trade num="1297">
    		<isin>XYZ0002</isin>
    		<quantity>2</quantity>
    		<price>72.99</price>
    		<customer>Customer2c</customer>
    	</trade>
    	<trade num="1298">
    		<isin>XYZ0003</isin>
    		<quantity>9</quantity>
    		<price>99.99</price>
    		<customer>Customer3</customer>
    	</trade>
    </records>
    my job.xml

    Code:
    ......
    
    <step id="importStep" parent="defaultStep">
    	<tasklet>
    		<chunk reader="reader" processor="processor"
    			writer="writer" commit-interval="1" skip-limit="0">
    		</chunk>
    	</tasklet>
    </step>
    
    ....
    
    
    <bean id="reader" class="org.springframework.batch.item.xml.StaxEventItemReader" scope="step">
            <property name="fragmentRootElementName" value="trade" />
            <property name="resource" value="file:src/test/resources/xmlFiles/my.xml" />
            <property name="unmarshaller" ref="customerMarshaller" />
        </bean>
        
        <bean id="customerMarshaller" class="org.springframework.oxm.xstream.XStreamMarshaller" scope="step">
            <property name="aliases">
                <util:map id="aliases">
                    <entry key="trade" value="batch.xxx.Customer" />
                </util:map>
            </property>
            <property name="useAttributeFor">
                <util:map id="useAttributeFor">
                  <entry key="num">
                    <value type="java.lang.Class">batch.xxx.Customer</value>
                  </entry>
                </util:map>
            </property> 
        </bean>
    
        <bean id="processor" class="batch.xxx.MyProcessor" scope="step"/>
        
        <bean id="writer" class="batch.xxx.PersonWriter"
            scope="step"/>
    my POJO:

    Code:
    public class Customer implements Serializable{
    
    	private static final long serialVersionUID = -1410817254992066867L;
    	
    	
    	private String num;
    	
    	private String isin;
    	private String quantity;
    	private String price;
    	private String customer;
    	
    	public String getIsin() {
    		return isin;
    	}
    	public void setIsin(String isin) {
    		this.isin = isin;
    	}
    	public String getQuantity() {
    		return quantity;
    	}
    	public void setQuantity(String quantity) {
    		this.quantity = quantity;
    	}
    	public String getPrice() {
    		return price;
    	}
    	public void setPrice(String price) {
    		this.price = price;
    	}
    	public String getCustomer() {
    		return customer;
    	}
    	public void setCustomer(String customer) {
    		this.customer = customer;
    	}
    	
    	public String getNum() {
    		return num;
    	}
    	public void setNum(String num) {
    		this.num = num;
    	}
    }

    In MyProcessor I get null for getNum() but for elements tag is OK.

    Please healp.
Working...
X