how to use substring in jstl

huzefaq
huzefaq used Ask the Experts™
on
Hi Guys

From the databse I am getting four columns. One of the colums is of type CLOB. I am converting this data to a string. Now this String text has lot of nodes, now what I need is to show only the values of between <Description locale="en_US" type="admin"> and </Description>.

Would anyone know how I can use substring to display only description value. I have pasted a sample of how the Column data will be and also the jstl code of how I show the values.
I would reallly appreciate any help regarding this

<c:forEach var="elements" items="${reportResult}">
            <tr>
                       <c:forTokens items="${elements}" delims="," var="current" varStatus="sts">

                              <c:if test ="${sts.count==1}">
                                 <td width = "50px" align="left" class="alt-color" >
                                    <c:out value="${current}" />
                              </c:if>
                              .....

                              <c:if test ="${sts.count==5}">  
                                    <td width = "290px" align="left" class="alt-color" >
                                  <c:out value="${current}" />
                              </c:if>
                              
                  </c:forTokens>

----------------------------------------------------

<?xml version="1.0" encoding="UTF-8"?><Promotion impl="com.ibm.commerce.marketing.promotion.DefaultPromotion"><PromotionKey><PromotionName>C1</PromotionName><StoreKey><DN>ou=b2c,o=seller organization,o=root organization</DN><Identifier>srsstore1</Identifier></StoreKey><Version>1</Version><Revision>2</Revision></PromotionKey><PromotionGroupKey><GroupName>ProductLevelPromotion</GroupName><StoreKey><DN>ou=b2c,o=seller organization,o=root organization</DN><Identifier>srsstore1</Identifier></StoreKey></PromotionGroupKey><TypedNLDescription impl="com.ibm.commerce.marketing.promotion.TypedNLDescription"><DefaultLocale>en_US</DefaultLocale><Description locale="en_US" type="admin">Get 40% of the highest priced item in the shop cart</Description><Description locale="en_US" type="long">Get 40% of the highest priced item in the shop cart</Description><Description locale="en_US" type="short">Get 40% of the highest priced item in the shop cart</Description></TypedNLDescription><Priority>200</Priority><Exclusive>0</Exclusive><ExemptPolicyList/><ExplicitlyAppliedPolicyList/><Status>1</Status><LastUpdate>20-04-2007 07:54:59</LastUpdate><LastUpdateBy><CustomerKey><LogonId>wcsadmin</LogonId></CustomerKey></LastUpdateBy><PerOrderLimit>1</PerOrderLimit><PerShopperLimit>-1</PerShopperLimit><ApplicationLimit>-1</ApplicationLimit><TargetSales>0</TargetSales><CorrespondingRBDTypeName>ItemLevelPercentDiscount</CorrespondingRBDTypeName><Schedule impl="com.ibm.commerce.marketing.promotion.schedule.PromotionSchedule"><DateRange impl="com.ibm.commerce.marketing.promotion.schedule.DateRangeSchedule"> ...
Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®

Commented:
String searchFor = "<Description locale=\"en_US\" type=\"admin\">";
String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Promotion impl=\"com.ibm.commerce.marketing.promotion.DefaultPromotion\"><PromotionKey><PromotionName>C1</PromotionName><StoreKey><DN>ou=b2c,o=seller organization,o=root organization</DN><Identifier>srsstore1</Identifier></StoreKey><Version>1</Version><Revision>2</Revision></PromotionKey><PromotionGroupKey><GroupName>ProductLevelPromotion</GroupName><StoreKey><DN>ou=b2c,o=seller organization,o=root organization</DN><Identifier>srsstore1</Identifier></StoreKey></PromotionGroupKey><TypedNLDescription impl=\"com.ibm.commerce.marketing.promotion.TypedNLDescription\"><DefaultLocale>en_US</DefaultLocale><Description locale=\"en_US\" type=\"admin\">Get 40% of the highest priced item in the shop cart</Description><Description locale=\"en_US\" type=\"long\">Get 40% of the highest priced item in the shop cart</Description><Description locale=\"en_US\" type=\"short\">Get 40% of the highest priced item in the shop cart</Description></TypedNLDescription><Priority>200</Priority><Exclusive>0</Exclusive><ExemptPolicyList/><ExplicitlyAppliedPolicyList/><Status>1</Status><LastUpdate>20-04-2007 07:54:59</LastUpdate><LastUpdateBy><CustomerKey><LogonId>wcsadmin</LogonId></CustomerKey></LastUpdateBy><PerOrderLimit>1</PerOrderLimit><PerShopperLimit>-1</PerShopperLimit><ApplicationLimit>-1</ApplicationLimit><TargetSales>0</TargetSales><CorrespondingRBDTypeName>ItemLevelPercentDiscount</CorrespondingRBDTypeName><Schedule impl=\"com.ibm.commerce.marketing.promotion.schedule.PromotionSchedule\"><DateRange impl=\"com.ibm.commerce.marketing.promotion.schedule.DateRangeSchedule\">";
int whereFound = xml.indexOf(searchFor);
String myDesc = searchFor + " not found!";
if(whereFound > -1) {
      whereFound += searchFor.length();
      myDesc = xml.substring(whereFound, xml.indexOf("</Description>", whereFound));
}
System.out.println("What I Found = " + myDesc);

Commented:
Sorry, previous post difficult to read.

String searchFor = "<Description locale=\"en_US\" type=\"admin\">";

String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><Promotion impl=\"com.ibm.commerce.marketing.promotion.DefaultPromotion\"><PromotionKey><PromotionName>C1</PromotionName><StoreKey><DN>ou=b2c,o=seller organization,o=root organization</DN><Identifier>srsstore1</Identifier></StoreKey><Version>1</Version><Revision>2</Revision></PromotionKey><PromotionGroupKey><GroupName>ProductLevelPromotion</GroupName><StoreKey><DN>ou=b2c,o=seller organization,o=root organization</DN><Identifier>srsstore1</Identifier></StoreKey></PromotionGroupKey><TypedNLDescription impl=\"com.ibm.commerce.marketing.promotion.TypedNLDescription\"><DefaultLocale>en_US</DefaultLocale><Description locale=\"en_US\" type=\"admin\">Get 40% of the highest priced item in the shop cart</Description><Description locale=\"en_US\" type=\"long\">Get 40% of the highest priced item in the shop cart</Description><Description locale=\"en_US\" type=\"short\">Get 40% of the highest priced item in the shop cart</Description></TypedNLDescription><Priority>200</Priority><Exclusive>0</Exclusive><ExemptPolicyList/><ExplicitlyAppliedPolicyList/><Status>1</Status><LastUpdate>20-04-2007 07:54:59</LastUpdate><LastUpdateBy><CustomerKey><LogonId>wcsadmin</LogonId></CustomerKey></LastUpdateBy><PerOrderLimit>1</PerOrderLimit><PerShopperLimit>-1</PerShopperLimit><ApplicationLimit>-1</ApplicationLimit><TargetSales>0</TargetSales><CorrespondingRBDTypeName>ItemLevelPercentDiscount</CorrespondingRBDTypeName><Schedule impl=\"com.ibm.commerce.marketing.promotion.schedule.PromotionSchedule\"><DateRange impl=\"com.ibm.commerce.marketing.promotion.schedule.DateRangeSchedule\">";

int whereFound = xml.indexOf(searchFor);
String myDesc = searchFor + " not found!";
if(whereFound > -1) {
      whereFound += searchFor.length();
      myDesc = xml.substring(whereFound, xml.indexOf("</Description>", whereFound));
}

System.out.println("What I Found = " + myDesc);

Author

Commented:
Hi ForceRs

The code which you pasted seems like to be in a scriptlet. Now what i needed is use the code inside jstl.
Because i will be getting the value like this

<c:if test ="${sts.count==5}">  
                                    <td width = "290px" align="left" class="alt-color" >
                                  <c:out value="${current}" />
                              </c:if>

Is there a way I can use jstl to do what you wrote before

Thanks for all your help
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

Commented:
You can use jstl functions.
<%@ taglib uri="http://java.sun.com/jsp/jstl/functions" prefix="fn" %>
It has all the necessary methods for doing substring, split etc.

Commented:
Like this:
${fn:substring(message, fn:indexOf(message, ’<Description>’)+1, fn:indexOf(text, ’</Description>’))}

Commented:
Oops.. it should be.
${fn:substring(message, fn:indexOf(message, ’<Description>’)+1, fn:indexOf(message, ’</Description>’))}

Also, there are other EL functions like substringAfter, substringBefore, etc. Check them on the JSTL.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial