?
Solved

how to get constants from xml in java

Posted on 2006-07-05
10
Medium Priority
?
232 Views
Last Modified: 2010-03-31

                  i have a xml like this;i have java constants file like Constants.DOCUMENT="DOC" so i mention like this xml;
                  <action url="x.jsp?map=Constants.DOCUMENT">Modify</uiName>

in java used DOM then get the url;      in java i want url like this x.jsp?map=DOC;now i am getting x.jsp?map=Constants.DOCUMENT
0
Comment
Question by:chaitu chaitu
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 3
  • 3
10 Comments
 
LVL 92

Expert Comment

by:objects
ID: 17040928
not sure I follow, did you want change the xml or after it is extracted??
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 17041082
>>         <action url="x.jsp?map=Constants.DOCUMENT">Modify</uiName>
this is not correct XML.
did you mean :
<action url="x.jsp?map=<%=Constants.DOCUMENT%>"><uiName>Modify</uiName></action>
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 17041096
how will u put scriptlet in xml file?
<action url="x.jsp?map=<%=Constants.DOCUMENT%>"><uiName>Modify</uiName></action>

0
Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

 
LVL 92

Expert Comment

by:objects
ID: 17041111
depends how u are generating the xml
0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 17041123
i write one xml file;in that one node is like this.i am reading the xml file in java  ;after reading i want to url should be url=x.jsp?map=Constants.DOCUMENT
<action url="x.jsp?map=Constants.DOCUMENT">Modify</uiName>
0
 
LVL 92

Expert Comment

by:objects
ID: 17041141
when you write the xml why aren't you then inserting the value of Contants.DOCUMENT
what code do u write it with?
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 17041205
>>       <action url="x.jsp?map=Constants.DOCUMENT">Modify</uiName>

In any case, uiName can't close the action tag ->
  <action url="x.jsp?map=Constants.DOCUMENT"><uiName>Modify</uiName></action>

You can do replacement using class reflection

String getConst(String name)
{
   int i=name.lastIndexOf('.');
   if (i<0) return name;
   try{
      return String.valueOf(
                 Class.forName(name.substring(0,i))
                    .getDeclaredField(name.substring(i+1))
                    .get(null)
               );
   }catch(Exception ex){ /* not found */ return name; }
}

0
 
LVL 20

Author Comment

by:chaitu chaitu
ID: 17041213
whatever value is there in Constants.DOCUMENT,it should come in place of Constants.DOCUMENT;
0
 
LVL 13

Accepted Solution

by:
Webstorm earned 2000 total points
ID: 17041241

Then replace constant value in url :

String replaceUrl(String url)
{
    int s=url.indexOf('?'),i,j;
    while(s>=0)
    {
         s++;
         i=url.indexOf('&',s);
         j=url.indexOf('=',s);
         if ((j>0)&&((i<0)||(j<i)))
         {
             String t=getConst((i<0)?url.substring(j+1):url.substring(j+1,i));
             url=url.substring(0,j+1)+t+((i<0)?"":url.substring(i));
             if (i>=0) i=j+1+t.length();
         }
         s=i;
    }
    return url;
}
0
 
LVL 13

Expert Comment

by:Webstorm
ID: 17043307
:-)
0

Featured Post

Optimize your web performance

What's in the eBook?
- Full list of reasons for poor performance
- Ultimate measures to speed things up
- Primary web monitoring types
- KPIs you should be monitoring in order to increase your ROI

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

For beginner Java programmers or at least those new to the Eclipse IDE, the following tutorial will show some (four) ways in which you can import your Java projects to your Eclipse workbench. Introduction While learning Java can be done with…
Introduction This article is the first of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article explains our test automation goals. Then rationale is given for the tools we use to a…
Video by: Michael
Viewers learn about how to reduce the potential repetitiveness of coding in main by developing methods to perform specific tasks for their program. Additionally, objects are introduced for the purpose of learning how to call methods in Java. Define …
The viewer will learn how to implement Singleton Design Pattern in Java.
Suggested Courses
Course of the Month10 days, 7 hours left to enroll

765 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question