Solved

how to get constants from xml in java

Posted on 2006-07-05
10
220 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
  • 4
  • 3
  • 3
10 Comments
 
LVL 92

Expert Comment

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

Expert Comment

by:Webstorm
Comment Utility
>>         <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
Comment Utility
how will u put scriptlet in xml file?
<action url="x.jsp?map=<%=Constants.DOCUMENT%>"><uiName>Modify</uiName></action>

0
 
LVL 92

Expert Comment

by:objects
Comment Utility
depends how u are generating the xml
0
 
LVL 20

Author Comment

by:chaitu chaitu
Comment Utility
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
Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

 
LVL 92

Expert Comment

by:objects
Comment Utility
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
Comment Utility
>>       <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
Comment Utility
whatever value is there in Constants.DOCUMENT,it should come in place of Constants.DOCUMENT;
0
 
LVL 13

Accepted Solution

by:
Webstorm earned 500 total points
Comment Utility

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
Comment Utility
:-)
0

Featured Post

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Java functions are among the best things for programmers to work with as Java sites can be very easy to read and prepare. Java especially simplifies many processes in the coding industry as it helps integrate many forms of technology and different d…
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
Viewers learn about the “while” loop and how to utilize it correctly in Java. Additionally, viewers begin exploring how to include conditional statements within a while loop and avoid an endless loop. Define While Loop: Basic Example: Explanatio…
This tutorial explains how to use the VisualVM tool for the Java platform application. This video goes into detail on the Threads, Sampler, and Profiler tabs.

762 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

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now