xml


How to read header information using StaxEventItemReader in spring batch


I am using StaxEventItemReader in Spring batch to read XML data and map it to java objects. I am able to reader repeating elements in XML successfully but problem is I want to read header information from my XML. Is there any way or support from StaxEventItemReader to read header information as well.
<?xml version="1.0" encoding="UTF-8"?>
<Contracts>
<TransactionInfo><Receiver>
<Code>CAT</Code>
<Name>CAT</Name>
</Receiver>
<SourceFile>
<FilePath>\\nft2a\ft$\transfer\EAD7SRR1.20160510_02363859.xml
</FilePath>
<FileName>EAD7SRR1.20160510_02363859.xml
</FileName>
<FileType>XML</FileType>
<FileTime>2016-05-10T02:36:41.337-07:00
</FileTime>
</SourceFile>
</TransactionInfo>
<Contract ContractID="Contract Rate Feed Test - LCL"
Carrier="DHL Danzas Air & Ocean" TermBegin="2015-09-15" TermEnd="2016-09-14"
CarrierOrgID="20455">
<LineItem ReferenceID="135516504144" Origin="Antwerp, BE" OriginCity="Antwerp" DestinationCity="Kpeme">
<Surcharge Amount="25.0" Basis="BILLADING" Code="CUSTOM" Name="Document Turnover Fee" CurrencyCode="USD" />
<Surcharge Amount="10.0" Basis="KILO" Code="CUSTOM" Name="Hazardous" CurrencyCode="USD" />
</LineItem>
<LineItem ReferenceID="135516504145" Origin="Antwerp, BE"OriginCity="Antwerp" DestinationCity="Libreville">
<Surcharge Amount="8.0" Basis="KILO" Code="CUSTOM" Name="Origin Handling"
CurrencyCode="USD" />
</LineItem>
</Contract>
<Contract ContractID="Contract_1005_Test_LCL"
GTNContractID="66860" CarrierOrgID="20455">
<LineItem ReferenceID="135516559003" Origin="Hong Kong, HK" DestinationCity="Hamburg">
<Surcharge Amount="10.0" Basis="KILO" Code="ACD" Name="Arbitraries Charge (Destination)" CurrencyCode="USD" />
</LineItem>
</Contract>
</Contracts>
How can I read TransactionInfo
My configuration is
<bean id="contractReader" class="org.springframework.batch.item.xml.StaxEventItemReader">
<property name="resource" value="classpath:sample-inputfile-srr-ocean.xml" />
<property name="fragmentRootElementName" value="Contract" />
<property name="unmarshaller">
<bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>com.cat.srr.gtn.domain.Contract</value>
</list>
</property>
</bean>
</property>
</bean>
<batch:job id="gtnContractJob">
<batch:step id="step1" next="step2">
<tasklet ref="ftpPullTask" />
</batch:step>
<batch:step id="step2">
<batch:tasklet transaction-manager="transactionManager">
<batch:chunk reader="multiResourceReader" writer="contractWriter"
processor="contractProcessor" commit-interval="10" />
</batch:tasklet>
</batch:step>
<batch:listeners>
<batch:listener ref="jobListener" />
</batch:listeners>
</batch:job>
Please help me how can i read header information using spring batch.
Use below property of StaxEventItemReader
fragmentRootElementNames
So your contractReader should look like this
<bean id="contractReader" class="org.springframework.batch.item.xml.StaxEventItemReader">
<property name="resource" value="classpath:sample-inputfile-srr-ocean.xml" />
<property name="fragmentRootElementNames" value="TransactionInfo,Contract" />
<property name="unmarshaller">
<bean class="org.springframework.oxm.jaxb.Jaxb2Marshaller">
<property name="classesToBeBound">
<list>
<value>com.cat.srr.gtn.domain.Contract</value>
<!-- Assumed that TransactionInfo is in same package as Contract -->
<value>com.cat.srr.gtn.domain.TransactionInfo</value>
</list>
</property>
</bean>
</property>
</bean>
Now how to handle it in processor is up to you

Related Links

how to read data from an XML site and put it in a var in vb.net
XQuery request opens whole file
getting parent node according two child node text XPATH
identity constraint key matches more than one value within the scope of its selector
Where to find/how to extract image hyperlinks from zipped docx document?
How do you echo a question mark in a shell script?
While transforming a xml to xml using xsl 1.0. xsl:attribute: Cannot add attributes to an element if children have been already added to the element
Can you declare and use a variable inside an XML without using XSL to transform/parse the XML
lxml and xml namespaces - Using find and findall to get XML Tag Value
cvc-complex-type.2.4.d: Invalid content was found starting with element 'testResources'. No child element is expected at this point
Using a webservice in Delphi by manually building XML (i.e. bypassing WSDL)
Validate Xml with XSd without using XSD physical path
How do I add line breaks after each tag in MSXML?
writing groovy script for REST service in SOAP UI
Logic for xsl:for is not clear
Dynamic cron Magento

Categories

HOME
uml
django
grok
fuelux
forms
serialization
newrelic
pearson-correlation
odoo-8
title
lanczos
fosrestbundle
g++
line
filechannel
mongodb-csharp
parameter-passing
red5
video-editing
jersey-1.0
blade
babel
angular-routing
perlbrew
slidetoggle
appirater
spotipy
bringtofront
tizen-tv
scrollview
jmockit
laravel-eloquent
instantiation
openwhisk
installscript
browser-extension
mnist
getlasterror
salesforce-communities
git-rewrite-history
linq-to-objects
plaintext
watchface
textsum
yii2-api
insight
convex-optimization
trojan
google-photos
hadoop-2.7.2
segment-io
tinkerpop3
file-uri
firebase-security
lean
piracy-prevention
mobiledoc-kit
concept-insights
rasterize
qt5.2
batik
finalbuilder
skspritenode
mmwormhole
code-documentation
grouping-sets
boost-program-options
metatrader5
punycode
machine-instruction
insight.database
verold
grails-plugin-rabbitmq
buffering
high-resolution
scriptaculous
mt4j
modular
time.h
scmmanager
jquery-1.9
composite-component
pyunit
subgraph
lr
msr
cruisecontrol.rb
rowlex
firefox-3
ifilter
xslcompiledtransform

Resources

Mobile Apps Dev
Database Users
javascript
java
csharp
php
android
MS Developer
developer works
python
ios
c
html
jquery
RDBMS discuss
Cloud Virtualization
Database Dev&Adm
javascript
java
csharp
php
python
android
jquery
ruby
ios
html
Mobile App
Mobile App
Mobile App