Substring of a string based on condition - java

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/

Related

How to Parse XML Document By Class Name Using Java

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.

How does Spring convert a String into an array?

I have a standard Spring class that accepts a String array. I know that Spring will take care of splitting a comma separated String into an array, but I'm unaware of how it does it under the hood.
The question that led me to this is whether the Strings will become trimmed after being split, but I would like to know how to see this under the hood activity as well if other future issues arise.
<bean id="allServicersDelimitedLineTokenizer"
class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<property name="names" value="col1,col2,col3,col4" />
</bean>
is the same as?
<bean id="allServicersDelimitedLineTokenizer"
class="org.springframework.batch.item.file.transform.DelimitedLineTokenizer">
<property name="names" value="col1 , col2, col3, col4" />
</bean>

In my xml file comes a tip in eclipse ide

<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

Difference of property definition in component.xml (OSGI DS)

Could someone tell me the difference between this two property definitions?
<scr:component xmlns:scr="http://www.osgi.org/xmlns/scr/v1.1.0" name="blablaService">
<property name="dbUrl" type="String">jdbc:h2:url</property>
<property name="dbUser" type="String" value="user" />
</scr:component>
I'm using Dictionary properties = context.getProperties(); to get the values. It seems to be that if the property given surround with xxx is treated as an Object holding a String[1] and the property which value is specified in the value attribute is treated as an Object which is de facto a String. For the first example (String) throws and Exception as the latter does not.
I'm developing using Eclipse and the Component Context is from org.osgi.service.component.ComponentContext.
<property name="dbUser" type="String" value="user" /> yields a scalar String.
<property name="dbUrl" type="String">jdbc:h2:url</property> yields a String[1].
This is per the spec. See 112.4.6 in the Compendium Spec.

Using CSVParser to parse a tab-delimited file

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>

Categories

Resources