I need to parse a file which is tab delimited. I'm attempting to use CSVBeans version 0.7 to do this. In an XML configuration file, I have to pass a separator value to indicate how fields are delimited, as follows:
parser className="org.csvbeans.parsers.CSVParser"/>
</strategy>
<property name="separator" value="\t" />
<property name="noStartTag" value="true" />
<converters>
As shown above, I have tried value="\t" />, but it's not working. I have also tried '\\t' and '\t' but to no avail. What value should I use for a tab character?
folks it would be highly appreciated if you guys please let me know the solution specific to csv beans 0.7.1 jar .
In XML a tab character is represented as . So, your file should be like:
<parser className="org.csvbeans.parsers.CSVParser"/>
</strategy>
<property name="separator" value=" " />
<property name="noStartTag" value="true" />
<converters>
Related
I'm writing a parsing tool to compare the textual content of two bean XML files in Java. The text content changes and we need a way to run a script to make sure the textual content is the same. I know we have org.w3c.dom which has a method getElementsByTagName("tag_name") and that returns a node list in the XML document. I'm wondering if anyone knows of a way to do this using the class name? I've been searching around but haven't been able to solve this yet.
<bean class="com.mycompany.myText" id="Q1.4">
<property name="name">
<value>Q1.4</value>
</property>
<property name="text">
<value>This is text one</value>
</property>
<property name="retired">
<value>true</value>
</property>
</bean>
<bean class="com.mycompany.myText" id="Q1.5">
<property name="name">
<value>Q1.5</value>
</property>
<property name="text">
<value>This is text two</value>
</property>
<property name="retired">
<value>true</value>
</property>
</bean>
I can't use the 'bean' element name as there are several other beans whith non relevant stuff I need only the ones with the class com.mycompany.myText and the value I'm trying to extract is property name=text - the text content
Any help here would be appreciated. Also as a side note I should mention that we have no direct control of how the XML file is managed and structured as it's fed to us from a 3rd party.
As you say, you need to select tags by their attributes.
You could use XPath to achieve this. See those resources:
http://viralpatel.net/blogs/java-xml-xpath-tutorial-parse-xml/
https://stackoverflow.com/a/24220968/6377268
The expression would be something like this
/bean[#class='com.mycompany.myText']/property[#name='text']/value
I hope I could help at least a little bit.
I am new to Spring and trying to read a complex file using FlatFileItemReader in Spring batch. The input file has structure as below. The sample below is one records and file has many records like this. Line starting with H is a Header Record and with F is a footer records. In between there are transaction records of type 10500, 10501, 405, 505. I am able to read this file using MultiLineItemReader.
I have created FieldSetMapper and Tokenizer and able to parse the file and get to individual fields. Only issue I am facing is that for records types 10500 and 10501 they can appear multiple times in between Header and Footer and when I am reading the file I am just getting the last read values and the previous records are just not appearing in reading. I know I am doing something silly but not able to understand. Has anybody had any experience reading such type of file.
H00025888222 444233DD 33232323232
105000000 0000 0
10501 1 2222222
105000000 0000 1
10501 1 2222223
40500 1222222
50500 21
F00555 5555 0
If the you just have multiple types of lines, but the lines are each standalone, then PatternMatchingCompositeLineMapper is probably what you're looking for.
<bean id="itemReader" class="org.springframework.batch.item.file.FlatFileItemReader" scope="step">
<property name="resource" value="#{jobParameters['input.file']}" />
<property name="lineMapper" ref="patternMatchingMapper" />
<property name="strict" value="true" />
</bean>
<bean id="patternMatchingMapper" class="org.springframework.batch.item.file.mapping.PatternMatchingCompositeLineMapper">
<property name="tokenizers">
<map>
<entry key="LINEA*" value-ref="tokenizerA" />
<entry key="LINEB*" value-ref="tokenizerB" />
</map>
</property>
<property name="fieldSetMappers">
<map>
<entry key="*" value-ref="beanWrapperFieldSetMapper" />
</map>
</property>
</bean>
If they are related (multiple lines need to be combined to make one object, you make consider SingleItemPeekableItemReader, described briefly here).
<bean id="cacheRedisConnectionFactory" class="org.springframework.data.redis.connection.jedis.JedisConnectionFactory"
destroy-method="destroy">
<property name="database" value="1" />
<property name="password" value="ms25J23#RA1~*3&c" />
</bean>
In this line : <property name="password" value="ms25J23#RA1~*3&c" />
Eclipse is telling me : The reference to entity "c" must end with the ';' delimiter.
Your XML is simply invalid. There are only 5 special characters in XML: ", ', &, < and >. If you use these characters inside a text content of your XML doc you can escape them as entity (& => &; amp like ampersand).
PS: Please, remove the spring-tag.
The ampersand & is a special character in XML. So, if you want to have an ampersand as value in XML, you need to escape it.
Ampersand in XML is an entity reference with the following format:
&#nnn;
&#hhh;
&name;
As you can see, the format is closed with a ;.
So, in order to have & as a value, escape it to &. This is recognised by XML engine as &. So, here is the valid XML element.
<property name="password" value="ms25J23#RA1~*3&c" />
Reference: List of XML and HTML character entity references
I have a string like below (read from a file and stored in a String)
<textmessages>
<textMessage timestamp="1424708212905">
<property name="tcs_car_kind" value="M32"/>
<property name="prev_cmdy_abrv" value="AUTOS"/>
<text><![CDATA[event_code="AP"]]></text>
</textMessage>
<textMessage timestamp="1424708212902">
<property name="shp_prim" value=""/>
<property name="prev_cmdy_abrv" value="AUTOS"/>
<text><![CDATA[event_code="CP"]]></text>
</textMessage>
<textMessage timestamp="1424708212902">
<property name="co_part_frm_nbr" value=""/>
<property name="prev_cmdy_abrv" value="AUTOS"/>
<text><![CDATA[event_code="LP"]]></text>
</textMessage>
</textmessages>
Requirement:
If string values matches to 'event_code="CP"' then I need to return complete data in between <textmessage> ---- </textmessage> as shown below.
<textMessage timestamp="1424708212902">
<property name="co_part_frm_nbr" value=""/>
<property name="shp_prim" value=""/>
<property name="prev_cmdy_abrv" value="AUTOS"/>
<text><![CDATA[event_code="CP"]]></text>
</textMessage>
A combination of those might help http://docs.oracle.com/javase/7/docs/api/java/util/regex/Pattern.html
http://docs.oracle.com/javase/7/docs/api/java/util/regex/Matcher.html
Search for 'event_code="CP"
with find() or match()
if is there...
Pattern p = Pattern.compile("pattern here");
if(p.matcher(content).find()){...}
Other option is to use xml parses, but regex are really simple for what you want.
if the pattern is really constant, yo can even use indexOf to go faster on your searches...
http://docs.oracle.com/javase/7/docs/api/java/lang/String.html#indexOf(java.lang.String)
if they are on the same location, then just reading from there is enough..
Why don't you try to parse that String as a XML using DOM?
With this you just have to compare the value of each "text" node and if it's value matches you return the whole parent node, as String for example...
Here is a basic sample of how it DOM works in Java: http://www.mkyong.com/java/how-to-read-xml-file-in-java-dom-parser/
Here's the problem in a nutshell:
<bean id="handlerFactory" class="com.westfieldgrp.audit.jdklogging.cpm.CPMHandlerFactory">
<property name="schemaName" value="${env.audit.databaseSchema}" />
<property name="bufferSize" value="${env.audit.bufferSize}" />
<property name="threaded" value="${env.audit.threadedAuditHandler}" />
<property name="dataSourceSelector" ref="dataSourceSelector" />
</bean>
bufferSize on the CPMHandlerFactory is an int. Spring is failing because it is trying to set the value to '${env.audit.bufferSize}' instead of the actual value from the properties file.
Now, when I change the name of the properties file or env.audit.bufferSize in the file, Spring complains that it can't find the property 'env.audit.bufferSize'. This says to me that it CAN find the property, but instead of setting the value to '20', it's trying to set it to '${env.audit.bufferSize}'. Can anyone explain why Spring might be doing this and what I can do about it?
Contents of the properties file below:
env.audit.databaseSchema=TDB2DATA
env.audit.dataSourceName=java:comp/env/AuditDataSourceAlias
env.audit.bufferSize=20
env.audit.threadedAuditHandler=true
Thanks,
Peter
EDIT:
Found the problem thanks to Jamestastic below. Here's what it was:
We have a "master" context file that looks like so:
<import resource="environmentBeans.xml" />
<import resource="serviceBeans.xml" />
<import resource="auditBeans.xml" />
The 'environmentBeans.xml' has the PropertyPlaceholderConfigurer in it. The problem was, I added some code that referenced the 'auditBeans.xml' context, which of course doesn't have the configurer. I switched it to reference the 'master' context and it works great.
The key was understanding why the values wouldn't get substituted out: because there was no property configurer.
So, Thanks!
Did you remember to add <context:property-placeholder /> or a PropertyPlaceholderConfigurer bean definition to your Spring context?