I have this xml file, I want to extract all xelement having number value in it attibute using XPath
<XtraSerializer version="1.0" application="TabbedView">
<property name="Items" iskey="true" value="5">
<property name="Item1" isnull="true" iskey="true">
<property name="Name">5d297655-eee4-4ab5-baa7-7d12964ac1ad</property>
<property name="TypeName">Document</property>
</property>
<property name="Item3" isnull="true" iskey="true">
<property name="Name">48575907</property>
<property name="TypeName">DockingContainer</property>
</property>
<property name="Item4" isnull="true" iskey="true">
<property name="Index">0</property>
<property name="Name">63403262</property>
<property name="TypeName">DockingContainer</property>
</property>
</property>
</XtraSerializer>
My result must be:
<property name="Name">48575907</property>
<property name="Name">63403262</property>
I use this syntax //property[text()=number]
in the tester https://www.freeformatter.com/xpath-tester.html#ad-output
But it give me NO MATCH!
What is wrong with the syntax I used to achieve the goal?
Try this XPATH that returns property node with integer value that not equal to 0
//property[number(text()) != 0]
if you want to fetch zero-value also:
//property[number(text()) != 0 or number(text()) = 0]
Related
Need to get the schema name from properties file for spring batch application.
Where the schema name is different for dev and prod for MSSQL database.
Job configuration in xml as below
<bean id="dataItemWriter" class="org.springframework.batch.item.database.JdbcBatchItemWriter">
<property name="assertUpdates" value="true" />
<property name="itemPreparedStatementSetter">
<bean class="org.test.batch.model.ItemStatementMapper" />
</property>
<property name="sql" >
<value>
<![CDATA[
INSERT INTO dbo.EMPLOYEE
(PROJECT_NAME
,APP_NAME
,EMPLOYEE_NAME)
values (?,?,?)
]]>
</value>
</property>
<property name="dataSource" ref="dataDataSource" />
</bean>
The schema name "dbo" should be retrieved form proprieties file so that DEV and PROD this can be changed in configuration
I don't see the need to put the value in a CDATA block, there are no special xml characters in your query. Here is an example: https://github.com/spring-projects/spring-batch/blob/8762e3411557aaf887867f8d8594b01127538cb1/spring-batch-core/src/test/resources/org/springframework/batch/core/resource/ListPreparedStatementSetterTests-context.xml#L25. So in your case, it should be something like:
<bean id="dataItemWriter" class="org.springframework.batch.item.database.JdbcBatchItemWriter">
<property name="assertUpdates" value="true" />
<property name="itemPreparedStatementSetter">
<bean class="org.test.batch.model.ItemStatementMapper" />
</property>
<property name="sql" >
<value>
INSERT INTO dbo.EMPLOYEE (PROJECT_NAME ,APP_NAME ,EMPLOYEE_NAME) values (?,?,?)
</value>
</property>
<property name="dataSource" ref="dataDataSource" />
</bean>
can someone please let me know how to use oracle sequence in spring batch writer?.
I tried using custseq.nextval in the insert statement, but its failing.
<bean id="testSSRReader"
class="org.springframework.batch.item.database.JdbcCursorItemReader">
<property name="dataSource" ref="bconnectedDataSource" />
<property name="sql"
value="select CUST_USA_ID , CUST_FIRST_NAME , CUST_LAST_NAME from BL_CUSTOMER fetch first 100 rows only" />
<property name="rowMapper">
<bean class="com.macys.batch.rowmapper.TestSSRRowMapper" />
</property>
</bean>
<bean id="testSSRProcessor" class="com.macys.batch.processor.TestSSRProcessor" />
<bean id="testSSRWriter"
class="org.springframework.batch.item.database.JdbcBatchItemWriter">
<property name="dataSource" ref="ocDataSource" />
<property name="sql">
<value>
<![CDATA[
insert into TESTTABLESSR(CUSTOMER_ID,CUSTOMER_NAME,CITY)
VALUES (custseq.nextval,:firstName,:lastName)
]]>
</value>
</property>
<property name="itemSqlParameterSourceProvider" ref="itemSqlParameterSourceProvider" />
</bean>
I am trying to pass this arguments to the method call Calendar.add(Calendar.YEAR, -10) in the Spring config. How to pass Calendar.YEAR which is actually an int, but Spring config treats it as a String and throws error.
It works if I call without Spring like:
from = from.add(Calendar.YEAR, -10);
Config:
<bean id="currCalendar"
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetClass" value="java.util.Calendar"/>
<property name="staticMethod">
<value>java.util.Calendar.getInstance</value>
</property>
</bean>
<bean id="from1"
class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject">
<ref local="currCalendar"/>
</property>
<property name="targetMethod" value="add"/>
<property name="arguments">
<list>
<value type="int">currCalendar.YEAR</value>
<value type="int">-50</value>
</list>
</property>
</bean>
Please suggest how to do in Spring
You almost had it, use the #{} placeholder:
<list>
<value type="int">#{currCalendar.YEAR}</value>
<value type="int">-50</value>
</list>
I am very new to spring batch. I have requirement in which i have to read a file having a header(Field Names) record and data records
i have to validate 1st record (check the field names matching against set of predefined names)- note that this record need to be skipped- i mean should not be part of items in processor)
read and store rest of the field values to a POJO
if the field 'date' is empty , i need to set the default value as 'xxxx-yy-zz'
i am unable to 1st and 3rd requirement with batch
here is the sample reader XML. please help
<bean id="reader" class="org.springframework.batch.item.file.FlatFileItemReader">
<property name="resource" value="classpath:input/import" />
<property name="encoding" value="UTF-8" />
<property name="linesToSkip" value="1" />
<property name="lineMapper" ref="line.mapper"/>
</bean>
<bean id="line.mapper" class="org.springframework.batch.item.file.mapping .DefaultLineMapper">
<property name="lineTokenizer" ref="line.tokenizer"/>
<property name="fieldSetMapper" ref="fieldSet.enity.mapper"/>
</bean>
<bean id="line.tokenizer" class="org.springframework.batch.item.file.transfo rm.DelimitedLineTokenizer">
<property name="delimiter">
<util:constant static-field="org.springframework.batch.item.file.transfo rm.DelimitedLineTokenizer.DELIMITER_TAB"/>
</property>
<property name="names" value="id,date,age " />
<property name="strict" value="false"/>
</bean>
<bean id="fieldSet.enity.mapper" class="org.springframework.batch.item.file.mapping .BeanWrapperFieldSetMapper">
<property name="targetType" value="a.b.myPOJO"/>
<property name="customEditors">
<map>
<entry key="java.util.Date">
<bean class="org.springframework.beans.propertyeditors.C ustomDateEditor">
<constructor-arg>
<bean class="java.text.SimpleDateFormat">
<constructor-arg value="yyyy-mm-dd" />
</bean>
</constructor-arg>
<constructor-arg value="true" />
</bean>
</entry>
</map>
</property>
Create your own custom FieldSetMapper like below
CustomeFieldSetMapper implements FieldSetMapper<a.b.myPOJO> {
#Override
public a.b.myPOJO mapFieldSet(FieldSet fs) {
a.b.myPOJO myPOJO = new a.b.myPOJO();
if(fs.readString("date").isEmpty()){
myPOJO.setDate("xxxx-yy-zz");
}
return a.b.myPOJO;
}
}
You think you should do date set in ItemProcessor.
Also, if <property name="linesToSkip" value="1" /> not fill your requirements - extend FlatFileItemReader and validate first line manually in it.
I found on internet that problem is that soap request contain unicode char for ,,ctrl + v", which is illegal character in Xml. I dont know how this get into String, but I want simple to remove it on server side.
Can plase someone give me the point how to solve this issue?
I found this snippet :
XMLOutputFactory f = new WstxOutputFactory();
f.setProperty(WstxOutputProperties.P_OUTPUT_INVALID_CHAR_HANDLER,
new InvalidCharHandler.ReplacingHandler(' '));
XMLStreamWriter sw = f.createXMLStreamWriter(...);
Can someone tell me how to configure Spring for construction of WstxOutputFactory with this handler? - InvalidCharHandler.ReplacingHandler(' '). Thanks for advice.
The solution is pretty simple :
<jaxws:endpoint id="kservice"
implementor="#kostrounService"
address="/call_kostroun" >
<jaxws:properties>
<entry key="javax.xml.stream.XMLOutputFactory" valueref="xmlOutputFactory" />
</jaxws:properties>
</jaxws:endpoint>
<bean id="invalidCharHandler" class="com.ctc.wstx.api.InvalidCharHandler$ReplacingHandler">
<constructor-arg value=" "/>
</bean>
<bean id="xmlOutputFactory" class="com.ctc.wstx.stax.WstxOutputFactory"/>
<bean class="org.springframework.beans.factory.config.MethodInvokingFactoryBean">
<property name="targetObject">
<ref local="xmlOutputFactory" />
</property>
<property name="targetMethod">
<value>setProperty</value>
</property>
<property name="arguments">
<list>
<util:constant static-field="com.ctc.wstx.api.WstxOutputProperties.P_OUTPUT_INVALID_CHAR_HANDLER"/>
<ref bean="invalidCharHandler" />
</list>
</property>
</bean>
This snippet of configuration remove illegal characters from soap message, and app then run ;-)