First, demand analysis
Use spring batch to read and write XML files: Reading the product information from an XML file, simply processing it, and writing it to another XML file.
Second, the Code implementation
1. Code structure diagram:
2. Applicationcontext.xml
<?xml version= "1.0" encoding= "UTF-8"? ><beans xmlns= "Http://www.springframework.org/schema/beans" xmlns: Xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xmlns:tx= "http ://www.springframework.org/schema/tx "xmlns:aop=" HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP "xmlns:context=" http ://www.springframework.org/schema/context "xsi:schemalocation=" Http://www.springframework.org/schema/beans http ://www.springframework.org/schema/beans/spring-beans-3.1.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/TX/http Www.springframework.org/schema/tx/spring-tx-3.1.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP/HTTP Www.springframework.org/schema/aop/spring-aop-3.1.xsd Http://www.springframework.org/schema/context/HTTP Www.springframework.org/schema/context/spring-context-3.1.xsd "default-autowire=" ByName "> <!--Auto Scan path --<context:component-scan base-package= "COM.ZDP"/> <bean id= "Joblauncher" Class= "Org.springframework.batch.core.launch.support.SimpleJobLauncher" > <property name= "jobrepository" ref= "Jobrepository"/> </bean> <bean id= "jobrepository" class= "org.springframework.batch.core.repository.su Pport. Mapjobrepositoryfactorybean "/> <bean id=" TransactionManager "class=" Org.springframework.batch.support.transaction.ResourcelessTransactionManager "/></beans>
Base-package: Scanning spring annotations
Joblauncher: Start Job
Jobrepository: Provides persistence for the job
TransactionManager: Provides transaction management operations
3. Springbatch.xml
<?xml version= "1.0" encoding= "UTF-8"? ><bean:beans xmlns= "Http://www.springframework.org/schema/batch" Xmlns:bean= "Http://www.springframework.org/schema/beans" xmlns:xsi= "Http://www.w3.org/2001/XMLSchema-instance" xmlns:p= "http://www.springframework.org/schema/p" xmlns:tx= "Http://www.springframework.org/schema/tx" xmlns:aop= "Http://www.springframework.org/schema/aop" xmlns:context= "Http://www.springframework.org/schema/context" xmlns: Util= "Http://www.springframework.org/schema/util" xsi:schemalocation= "http://www.springframework.org/schema/ Beans Http://www.springframework.org/schema/beans/spring-beans-3.1.xsd Http://www.springframework.org/schema/tx Http://www.springframework.org/schema/tx/spring-tx-3.1.xsd HTTP://WWW.SPRINGFRAMEWORK.ORG/SCHEMA/AOP/HTTP Www.springframework.org/schema/aop/spring-aop-3.1.xsd Http://www.springframework.org/schema/context/HTTP Www.springframework.org/schema/context/spring-context-3.1.xsdhttp://www.springframework.org/schema/baTCH http://www.springframework.org/schema/batch/spring-batch-2.1.xsd Http://www.springframework.org/schema/util Http://www.springframework.org/schema/util/spring-util.xsd "> <bean:import resource=" applicationcontext.xml "/> <job id=" Xmlfilereadandwriterjob "> <step id=" xmlfilereadandwriterstep "> <taskl et> <chunk reader= "XmlReader" writer= "XmlWriter" processor= "xmlprocessor" commit-interval= "/>" </tasklet> </step> </job> <!--XML file reads--<bean:bean id= "XmlReader" class= "Org.springframework.batch.item.xml.StaxEventItemReader" scope= "step" > <bean:property name= "fragmentr Ootelementname "value=" Product "/> <bean:property name=" Unmarshaller "ref=" Trademarshaller "/> < Bean:property name= "Resource" value= "file:#{jobparameters[' Inputfilepath ']}"/> </bean:bean> <!--XML file write --<bean:bean id= "XmlWriter" Class= "Org.springframework.batch.item.xml.StaxEventItemWriter" scope= "step" > <bean:property name= " Roottagname "value=" ZDP "/> <bean:property name=" Marshaller "ref=" Trademarshaller "/> <bean:prop Erty name= "Resource" value= "file:#{jobparameters[' Outputfilepath ']}"/> </bean:bean> <bean:bean id= "Trad Emarshaller "class=" Org.springframework.oxm.xstream.XStreamMarshaller "> <bean:property name=" Aliases "> <util:map id= "aliases" > <bean:entry key= "Product" value= "Com.zdp.domain.Product"/> <bean:entry key= "Buydate" value= "java.util.Date"/> </util:map> </bean:proper Ty> </bean:bean></bean:beans>
1. The job contains a step,step that contains the basic read (XmlReader), processing (xmlprocessor), and write (XmlWriter).
2. XmlReader is configured with an XML read operation, and the resource property specifies the file path information. Know the file path . The Fragmentrootelementname property is the name of the specified root node. Unmarshaller is responsible for completing the parsing node information and mapping it into a program Pojo object.
< Span style= "Background-color:rgb (255,255,255)" >3. Trademarshaller to parse the XML node, where entry's key specifies the corresponding root node name product,value the Pojo class of the specified program, so that the program can associate the child nodes under the product node with the Pojo class (Product " to match, the contents of the child node are assigned to the properties of the Pojo class when matching the name of the child node to the property name in the Pojo class. This completes the read of a root node, and the framework controls the looping operation until all the roots in the file (product " node is all read out. This completes the read operation of the XML file.
4. XmlWriter configured the write operation to the XML file. The Resource property provides path information for the file. At the same time, also need to know this file with the node information, the Roottagname attribute provides root node name information. marshaller The tool to convert Pojo objects into XML fragments. The Unmarshaller and write operations of this article are marshaller with the same converter, because Xstreammarshaller provides the ability to convert node fragments to Pojo objects while providing the ability to persist Pojo objects into XML files.
4. Xmlprocessor
/** * XML file Processing class. */@Component ("Xmlprocessor") public class Xmlprocessor implements Itemprocessor<product, product> {// XML file content processing @overridepublic product process (product product) throws Exception {product.setbuydate (New Date ()); Product.setcustomer (Product.getcustomer () + "Customer!"); Product.setid (Product.getid () + "_1");p Roduct.setprice (Product.getprice () + 1000.0);p roduct.setquantity ( Product.getquantity () + +); return product;}}
5. Product
/** * Entity Products Class */public class Product {private string id;private int quantity;//quantity private double price;//Prices private string Customer Customer Private Date buydate; Date
6. Joblaunch
/** * Test Client */public class Joblaunch {public static void main (string[] args) {ApplicationContext context = new class Pathxmlapplicationcontext ("Springbatch.xml"); Joblauncher Joblauncher = (joblauncher) context.getbean ("Joblauncher"); Job Job = (Job) Context.getbean ("Xmlfilereadandwriterjob"); try {joblauncher.run (Job, New Jobparametersbuilder (). AddString ("Inputfilepath", "D:\\input.xml") . AddString ("Outputfilepath", "D:\\output.xml") . Tojobparameters ());} catch (Exception e) {e.printstacktrace ();}}}
7. Input.xml
8. Output.xml