Solved

how to get constants from xml in java

Posted on 2006-07-05
10
222 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
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
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

Courses: Start Training Online With Pros, Today

Brush up on the basics or master the advanced techniques required to earn essential industry certifications, with Courses. Enroll in a course and start learning today. Training topics range from Android App Dev to the Xen Virtualization Platform.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
Java SE 8u111  Lot of stuff broke 11 69
how to add new optional parameter to JSP 1 40
factorial example 4 39
spring jars download 1 26
After being asked a question last year, I went into one of my moods where I did some research and code just for the fun and learning of it all.  Subsequently, from this journey, I put together this article on "Range Searching Using Visual Basic.NET …
This was posted to the Netbeans forum a Feb, 2010 and I also sent it to Verisign. Who didn't help much in my struggles to get my application signed. ------------------------- Start The idea here is to target your cell phones with the correct…
This tutorial covers a step-by-step guide to install VisualVM launcher in eclipse.
This tutorial will introduce the viewer to VisualVM for the Java platform application. This video explains an example program and covers the Overview, Monitor, and Heap Dump tabs.

776 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