• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 416
  • Last Modified:

JTidy and javascript values

I would like to get the value of a javascript array.
I have parsed a page with JTidy and I do not know how to get the javascript value.
I know how to get items with specific ID or name but take a look how this code is.

<script type="text/javascript">
var options = {
'name_1': 'Item 1',
'name_2': 'Item 2',
'name_3': 'Item 3'
};
</script>


I want with JTidy to get the value of name_2 and name_3.
Is this possible?
0
fireburner69
Asked:
fireburner69
  • 5
  • 3
  • 2
1 Solution
 
leakim971PluritechnicianCommented:
Hello fireburner69,

Try :


value_name2 = options['name_2'];
value_name3 = options['name_3'];

Open in new window

0
 
leakim971PluritechnicianCommented:
Have a look to this code :

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<script language="javascript">
	var options = {
		'name_1': 'Item 1',
		'name_2': 'Item 2',
		'name_3': 'Item 3'
	};
	function dspArrayVal() {
		alert(options['name_1']);
		alert(options['name_2']);
		alert(options['name_3']);
	}
</script>
</head>
<body onload="dspArrayVal();">
</body>
</html>

Open in new window

0
 
fireburner69Author Commented:
We are talking about Netbeans :)
and the issue is that we parse the page with JTidy :)
So javascript is not what I want :)
0
Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

 
CEHJCommented:
You need to call parse or parseDOM and navigate to the Javascript nodes via the dom
0
 
fireburner69Author Commented:
And yet again nope!
Ok I tried another method and it worked for me thanks for your try people! :)
0
 
CEHJCommented:
>>And yet again nope!

No. I gave you a working solution over half an hour before you posted
0
 
fireburner69Author Commented:
I posted exacly the code i have!
And as your said I have to navigate to the javascript nodes via the dom but you know something JTIDY is not creating nodes for the javascript or the text is not as a xml structure!
If you provide a valid source code that JTidy can parse the javascript that I have provided above and return the value of name_2 and name_3 I will accept your solution.

If not then I believe that I was right and your solution is not correct.
Thanks again :)
0
 
fireburner69Author Commented:
What I wanted to do is to be able to parse the values from name_2 and name_3.
With JTidy I was unable to get that specific html code so I tried another way.

URL url = new URL("http://www.google.com");
BufferedReader in = new BufferedReader(new InputStreamReader(url.openStream()));
String inputLine;
while ((inputLine = in.readLine()) != null) source = source+inputLine;

And now I have the source that holds all the HTML source code.
After that point alot of replaceAll and split functions take place to split the string into an array that holds the values I want.

Although this method is not the best I admin that but I does my job for the moment.
Thanks again everyone for trying.
0
 
CEHJCommented:
Yes, you can do better than that. JTidy will

a. turn the source into xhtml
b. provide all the javascript as cdata sections

You can simply get a NodeList of CDATA sections (Java type CDATASection)
0
 
fireburner69Author Commented:
Ok but again as shown in the html source code how do I track down all the javascript elements?


In the attached file how can I make JTidy work and get the values I want?
Thanks again
<html>
<head>
</head>
<body>
<div id="nice">Some text</div>
<script language="javascript">
function do_nothing(){};
</script>
<div>Some more text</div>
<script language="javascript">
var options = {
'name_1': 'Item 1',
'name_2': 'Item 2',
'name_3': 'Item 3'
};
</script>
</body>
</html>

Open in new window

0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

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.

  • 5
  • 3
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now