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

xslt to rename nodes based on recursive position [closed]
cakephp how can I read data from a xml webservice?
What is meant by XML Validation?
XSLT to remove instances of element in XML
XML VB.net not finding property
grab data from xml file
Eclipse formatter for XML like Java
Error in Retrieving XML data into ArrayCollection through HTTPService in FLEX
Go xml unmarshalling
Convert Italic XML Tags into WordML Tags II
print XML/XSLT data via CGI
Need to parse a text file into a specific xml format
using xslt to sort xml via onclick function
xs:unique is matching multiple values when they are unique
Trouble writing to xml using Perl
Grouping elements on the same level

Categories

HOME
xml
turing-machines
prism
formal-languages
apache-spark-mllib
gradient
rom
square
jquery-ui
crystal-reports-2013
sendgrid-api-v3
java.util.logging
resultset
angular-dart
speech-synthesis
gkturnbasedmatch
selenium-ide
atlassian
magnetic-cards
symbol
altera
event-sourcing
feathersjs
selinux
lilypond
asciidoctor-pdf
dpdk
backpack-for-laravel
event-store
monit
resourcebundle
strongname
jboss6.x
beautifier
opentk
handler
bringtofront
twiml
testbed
android-gridview
reactjs.net
apiary.io
magento-1.9.2.4
salesforce-communities
google-knowledge-graph
amazon-ses
odroid
recurrent-neural-network
datacachefactory
magento-2.0.7
configurationmanager
vugen
x-ray
singleinstance
context-switch
avx
check-mk
cd-burning
javafx-css
cubism.js
greatest-n-per-group
hclust
http-status-code-401
spreadjs
phpwebsocket
sklearn-pandas
html-to-pdf
nupic
beanstalk
nio2
avisynth
sha512
baasbox
branch-and-bound
teamcity-9.1
diff3
cakephp-2.7
bcache
openbabel
infosphere-spl
dbaccess
gii
named-parameters
grails-plugin-rabbitmq
buffering
ecslidingviewcontroller
high-resolution
android-sdk-2.3
device-width
xcode3.2
wcf-configuration
cuda-gdb
lambdaj
inkcanvas
mapping-by-code
database-create
wiimote
infobright
box2d-iphone
relative
clrstoredprocedure
w3c-geolocation
timertask
dot-emacs
web-garden
zen
duplex
echo3
lxr
java1.4
information-management
wcf-proxy

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