Solved

get a value from a content string

Posted on 2004-04-25
10
285 Views
Last Modified: 2010-03-31
xmlurl=http%3A%2F%2F127.0.0.1%2Fmydbs%2Fwipdesign.nsf%2FagtGetDocsViewXML%3Fopenagent%26u%3Dc956395%26uname%3DJoe+Blogg%26start%3D1%26count%3D9999%26viewname%3Dfrmgicsissueview%26frmname%3D%26frmtype%3DView&xslurl=http%3A%2F%2F127.0.0.1%2Fmydbs%2Fwipdesign.nsf%2Fviewreport.xsl

I do have a method to decode the content string, however I need a method to pass in this content string and get the values for xmlurl and xslurl.

Thanks
pcorreya
0
Comment
Question by:pcorreya
  • 3
  • 2
  • 2
  • +1
10 Comments
 
LVL 7

Accepted Solution

by:
vbandaru earned 43 total points
ID: 10911717
The String Object has an overloaded method called indexOf. You should be able to use that in conjunction with substring to get the xmlurl and xslurl.

something like this.

String contentString="xmlurl=http%3A%2F%2F127.0.0.1%2Fmydbs%2Fwipdesign.nsf%2FagtGetDocsViewXML%3Fopenagent%26u%3Dc956395%26uname%3DJoe+Blogg%26start%3D1%26count%3D9999%26viewname%3Dfrmgicsissueview%26frmname%3D%26frmtype%3DView&xslurl=http%3A%2F%2F127.0.0.1%2Fmydbs%2Fwipdesign.nsf%2Fviewreport.xsl";

String xmlurl= contentString.substring(7,contentString.indexOf("xslurl")-1);

String xslurl= contentString.substring(contentString.indexOf("xslurl")+7);
0
 
LVL 86

Assisted Solution

by:CEHJ
CEHJ earned 41 total points
ID: 10912076
Or you can use regular expressions:

            String x = "xmlurl=http%3A%2F%2F127.0.0.1%2Fmydbs%2Fwipdesign.nsf%2FagtGetDocsViewXML%3Fopenagent%26u%3Dc956395%26uname%3DJoe+Blogg%26start%3D1%26count%3D9999%26viewname%3Dfrmgicsissueview%26frmname%3D%26frmtype%3DView&xslurl=http%3A%2F%2F127.0.0.1%2Fmydbs%2Fwipdesign.nsf%2Fviewreport.xsl";
            x = URLDecoder.decode(x, "UTF-8");
            final String RE = "xmlurl=(.+)\\?.+xslurl=(.+)";
            Pattern p = Pattern.compile(RE);
            Matcher m = p.matcher(x);
            if (m.matches()) {
                  System.out.println(m.group(1));
                  System.out.println(m.group(2));
            }
0
 
LVL 92

Expert Comment

by:objects
ID: 10914093
vbandaru is right, the only additional thing you may want to do is decode the urls:

import java.net.*;

public class extracturls
{
      public static void main(String[] args)
      {
            String contentString="xmlurl=http%3A%2F%2F127.0.0.1%2Fmydbs%2Fwipdesign.nsf%2FagtGetDocsViewXML%3Fopenagent%26u%3Dc956395%26uname%3DJoe+Blogg%26start%3D1%26count%3D9999%26viewname%3Dfrmgicsissueview%26frmname%3D%26frmtype%3DView&xslurl=http%3A%2F%2F127.0.0.1%2Fmydbs%2Fwipdesign.nsf%2Fviewreport.xsl";

            String xmlurl= URLDecoder.decode(contentString.substring(7,contentString.indexOf("xslurl")-1));

            String xslurl= URLDecoder.decode(contentString.substring(contentString.indexOf("xslurl")+7));
            
            System.out.println(xmlurl);
            System.out.println(xslurl);
      }
}
0
Industry Leaders: 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 86

Expert Comment

by:CEHJ
ID: 10914109
>>vbandaru is right

? So i'm wrong am i? ;-)

In any case, your comment is redundant - you obviously have not read the question:

>>I do have a method to decode the content string, ...
0
 
LVL 1

Author Comment

by:pcorreya
ID: 10914501
Thanks you all for your contribution, however it would be nice to have a class that I can use to pass it the content string and the lookup field and return its value.
0
 
LVL 92

Assisted Solution

by:objects
objects earned 41 total points
ID: 10914808
try something like:

     public static String extractField(String contentString, String lookup)
     {
          String[] pairs = contentString.split("&");
          for (int i=0; i<pairs.length; i++)
          {
              String[] keyvalue = pairs[i].split("=");
              if (keyvalue[0].equals(lookup)) return keyvalue[1];
          }
          return null;
     }
0
 
LVL 1

Author Comment

by:pcorreya
ID: 10927241
objects, i am getting the message Method split(java.lang.String) not found in class java.lang.String
0
 
LVL 92

Expert Comment

by:objects
ID: 10927304
it only became available in 1.4.
you'll need to use StringTokenizer in previous versions.
0

Featured Post

Technology Partners: 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!

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
how to see all occupied ports on windows 10 laptop 15 82
Adding multiple JVM environments to RedHat 6 7 69
Running JavaFX on the Raspberry Pi 27 120
junit example issue 2 22
INTRODUCTION Working with files is a moderately common task in Java.  For most projects hard coding the file names, using parameters in configuration files, or using command-line arguments is sufficient.   However, when your application has vi…
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…
Viewers will learn about arithmetic and Boolean expressions in Java and the logical operators used to create Boolean expressions. We will cover the symbols used for arithmetic expressions and define each logical operator and how to use them in Boole…
This tutorial covers a practical example of lazy loading technique and early loading technique in a Singleton Design Pattern.

685 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