I am trying to read xml file from remote location in my DAO.
<bean id="queryDAO"
class="com.contextweb.openapi.commons.dao.reporting.impl.QueryDAOImpl">
<property name="dataSource" ref="myDS"/>
<property name="inputSource" ref="xmlInputSource"/>
</bean>
<bean id="fileInputStream" class="java.io.FileInputStream">
<constructor-arg index="0" type="java.lang.String"
value="${queriesFileLocation}"/>
</bean>
<bean id="xmlInputSource" class="org.xml.sax.InputSource">
<constructor-arg index="0" >
<ref bean="fileInputStream"/>
</constructor-arg>
</bean>
I am able to read XML for first time. For subsequent requests, inputstream is exhausted.
You are using
FileInputStreamthats where the problem lies. Once you have read the data in the stream there is no way you can read the contents again. The stream has reached its end.Solution to this problem would be to use another class
BufferedInputStreamwhich supports resetting of stream pointing to anywhere in the file.Following example shows that
BufferedInputStreamopened only once and can be used to read the file multiple times.There is catch. Since you are not using this class yourself but dependent on
org.xml.sax.InputSourceto do it, you need to create your ownInputSourceby extending this class and overridegetCharacterStream()andgetByteStream()methods tomark()reset()the stream to start of the file.