1

I made a HttpClient call to a server and it responded with a response of HTML. In which I see something like

 <html lang="en"><head>
 <script type="text/javascript"><!--
  var testElement = "Y4Yyn%2bZXWcmpz2TadkTlyrc8yGI%3d";   var timeout_redirect_url = "/career?company=asd&loginFlowRequired=true&lang=en_US";
 //--></script>
 </html>

I have saved all the HTML content to a String.

Now I have all the HTML in

String res;

res contains all the HTML.

I need to get that testElement value. The response which I got is a string. How can I get the testElement value?

I'm trying to parse for jsoup to get the value but got struck. Can anyone help me out please. Thanks in advance.

2
  • The big problem here is that you want data, but get a html string back instead. If you have no way to get the data in non-html format, you're stuck with either searching the string for the text describing the variable and splice out the corrent part, (everything between var testElement = " and the closing ") which is quite error prone. Or you could create a new document, insert the html string into it and then insert another script tag that will expose the local variable testElement. (which can be complicated.) Commented Nov 19, 2015 at 15:05
  • check this out stackoverflow.com/questions/14904776/… Commented Nov 19, 2015 at 15:08

2 Answers 2

2

You can use regex to accomplish this:

public static void main(String[] args) {
    String test = "<html lang=\"en\"><head>" +
    "<script type=\"text/javascript\"><!--" +
    "var testElement = \"Y4Yyn%2bZXWcmpz2TadkTlyrc8yGI%3d\";   var timeout_redirect_url = \"/career?company=asd&loginFlowRequired=true&lang=en_US\";"+
    "//--></script>"+
    "</html>";
    Pattern pattern = Pattern.compile("(?<=testElement\\s=\\s\").*?(?=\")");

    Matcher matcher = pattern.matcher(test);
    while(matcher.find())
    {
        String s = matcher.group(0);
        System.out.println("" + s);
    }
}

Output:

Y4Yyn%2bZXWcmpz2TadkTlyrc8yGI%3d

Try it out hopefully that is what you are looking for...

Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. I have used split method and got the required output.
0

Actually whatever value you are getting in testElement variable I guess is a object, you need to get back the elements within it and parse it to get the expected output.

2 Comments

It is not the Object. I think it is a String. If it is a String do you know how to get that value?
Please provide more information, this is not adequate to answer your query.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.