Solved

how to get constants from xml in java

Posted on 2006-07-05
10
228 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

What Is Transaction Monitoring and who needs it?

Synthetic Transaction Monitoring that you need for the day to day, which ensures your business website keeps running optimally, and that there is no downtime to impact your customer experience.

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…
By the end of 1980s, object oriented programming using languages like C++, Simula69 and ObjectPascal gained momentum. It looked like programmers finally found the perfect language. C++ successfully combined the object oriented principles of Simula w…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This video teaches viewers about errors in exception handling.

729 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