Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

JTidy and javascript values

Posted on 2009-12-28
13
Medium Priority
?
408 Views
Last Modified: 2013-12-14
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
Comment
Question by:fireburner69
  • 5
  • 3
  • 2
10 Comments
 
LVL 83

Expert Comment

by:leakim971
ID: 26134878
Hello fireburner69,

Try :


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

Open in new window

0
 
LVL 83

Expert Comment

by:leakim971
ID: 26134889
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
 
LVL 1

Author Comment

by:fireburner69
ID: 26134933
We are talking about Netbeans :)
and the issue is that we parse the page with JTidy :)
So javascript is not what I want :)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
LVL 86

Expert Comment

by:CEHJ
ID: 26137380
You need to call parse or parseDOM and navigate to the Javascript nodes via the dom
0
 
LVL 1

Author Comment

by:fireburner69
ID: 26137581
And yet again nope!
Ok I tried another method and it worked for me thanks for your try people! :)
0
 
LVL 86

Expert Comment

by:CEHJ
ID: 26137633
>>And yet again nope!

No. I gave you a working solution over half an hour before you posted
0
 
LVL 1

Author Comment

by:fireburner69
ID: 26137724
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
 
LVL 1

Accepted Solution

by:
fireburner69 earned 0 total points
ID: 26143885
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
 
LVL 86

Expert Comment

by:CEHJ
ID: 26148162
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
 
LVL 1

Author Comment

by:fireburner69
ID: 26149144
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

Featured Post

Hire Technology Freelancers with Gigs

Work with freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely, and get projects done right.

Question has a verified solution.

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

Jaspersoft Studio is a plugin for Eclipse that lets you create reports from a datasource.  In this article, we'll go over creating a report from a default template and setting up a datasource that connects to your database.
In this post we will learn how to connect and configure Android Device (Smartphone etc.) with Android Studio. After that we will run a simple Hello World Program.
This video teaches viewers about errors in exception handling.
Viewers will learn how to properly install Eclipse with the necessary JDK, and will take a look at an introductory Java program. Download Eclipse installation zip file: Extract files from zip file: Download and install JDK 8: Open Eclipse and …
Suggested Courses
Course of the Month20 days, 17 hours left to enroll

810 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