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
Solved

how to get constants from xml in java

Posted on 2006-07-05
10
224 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
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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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 500 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

Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Java had always been an easily readable and understandable language.  Some relatively recent changes in the language seem to be changing this pretty fast, and anyone that had not seen any Java code for the last 5 years will possibly have issues unde…
Introduction This article is the last of three articles that explain why and how the Experts Exchange QA Team does test automation for our web site. This article covers our test design approach and then goes through a simple test case example, how …
Viewers learn how to read error messages and identify possible mistakes that could cause hours of frustration. Coding is as much about debugging your code as it is about writing it. Define Error Message: Line Numbers: Type of Error: Break Down…
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.

856 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