Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

newbie question regarding parsing html file

Status
Not open for further replies.

Nostradamus

Technical User
May 3, 2000
419
SE
Hello.

I supervise a bunch of services. Each of these services can respond with their status and wellbeeing if I call upon them using a webapplikation in Apache Tomcat that generates simple html files.

For instance. I browse to to bring up the status for service number 123.

The webapplikation responds with a simple html page looking like this.

<HTML>
<BODY>
RecordID=123<BR>
Status=CLEAR<BR>
Name=Name of service<BR>
IpAddr=192.168.1.22<BR>
Descr=Description<BR>
</BODY>
</HTML>


What I would like to do is automatically parse this html file. Check the status line if it's "CLEAR" or "DOWN" and from there build up two new html pages with information about the service beeing up and running or down.

In the new html pages I want to be able to include information about description (Descr), IP-address (IpAddr) and so on.

How would I go about to do this? I have no idea what is possible using javascript, but can this be done?

Any input is greatly appreciated.
Thanks in advance.
 
Hi

I would start by rewriting the web application to return valid JSON or XML. If you have to go with that mess, then just parse it as plain text. Has not much in common with HTML anyway.

Regarding the JavaScript part, tell us how you intend to involve it. In meantime, this is how you can extract the status from there :
JavaScript:
status[teal]=[/teal]string_containing_that_text[teal].[/teal][COLOR=darkgoldenrod]match[/color][teal]([/teal][fuchsia]/Status=(\w+)/[/fuchsia][teal])[[/teal][purple]1[/purple][teal]][/teal]

Feherke.
 
the webapplication is owned and maintained be a third party supplier. It can't be changed, at least not easily. XML would be superb, but for now I'll have to manage with the html files.

How would I do the following.

On a webpage, list 10 different services with up or down status?

I want to be able to say

Service 1 is "up"
Service 2 is "down"
and so on?
That is, connect to get the proper status and use the information to build a new page.
I want this to check for new status messages every time the page reloads or have a configuration for this.
 
Hi

This AJAX solution will work on Gecko, Presto and WebKit. ( Old Trident will not handle [tt]addEventListener()[/tt]. I let the workaround at your decision. )
JavaScript:
[b]var[/b] service[teal]=[[/teal][purple]123[/purple][teal],[/teal][purple]456[/purple][teal],[/teal][purple]7[/purple][teal],[/teal][purple]8[/purple][teal],[/teal][purple]9[/purple][teal]][/teal] [gray]// the recid GET parameters[/gray]

window[teal].[/teal][COLOR=darkgoldenrod]addEventListener[/color][teal]([/teal][green][i]'load'[/i][/green][teal],[/teal][b]function[/b] [teal]()[/teal] [teal]{[/teal]
  [b]var[/b] list[teal]=[/teal]document[teal].[/teal][COLOR=darkgoldenrod]getElementById[/color][teal]([/teal][green][i]'statuslist'[/i][/green][teal])[/teal]
  [b]for[/b] [teal]([/teal][b]var[/b] i[teal]=[/teal][purple]0[/purple][teal],[/teal]l[teal]=[/teal]service[teal].[/teal]length[teal];[/teal]i[teal]<[/teal]l[teal];[/teal]i[teal]++)[/teal] [teal]{[/teal]
    [b]var[/b] item[teal]=[/teal]document[teal].[/teal][COLOR=darkgoldenrod]createElement[/color][teal]([/teal][green][i]'li'[/i][/green][teal])[/teal]
    item[teal].[/teal]innerHTML[teal]=[/teal][green][i]'service '[/i][/green][teal]+[/teal]service[teal][[/teal]i[teal]][/teal]
    list[teal].[/teal][COLOR=darkgoldenrod]appendChild[/color][teal]([/teal]item[teal])[/teal]
    service[teal][[/teal]i[teal]]=[[/teal]service[teal][[/teal]i[teal]],[/teal]item[teal]][/teal]
  [teal]}[/teal]
  [COLOR=darkgoldenrod]check[/color][teal]()[/teal]
  [COLOR=darkgoldenrod]setInterval[/color][teal]([/teal]check[teal],[/teal][purple]60000[/purple][teal])[/teal] [gray]// == 1 minute[/gray]
[teal]}[/teal][teal],[/teal][b]false[/b][teal])[/teal]

[b]function[/b] [COLOR=darkgoldenrod]check[/color][teal]()[/teal]
[teal]{[/teal]
  [b]for[/b] [teal]([/teal][b]var[/b] i[teal]=[/teal][purple]0[/purple][teal],[/teal]l[teal]=[/teal]service[teal].[/teal]length[teal];[/teal]i[teal]<[/teal]l[teal];[/teal]i[teal]++)[/teal] [teal]{[/teal]
    [b]var[/b] http[teal]=[/teal][b]new[/b] [COLOR=darkgoldenrod]XMLHttpRequest[/color][teal]()[/teal]
    http[teal].[/teal][COLOR=darkgoldenrod]open[/color][teal]([/teal][green][i]'GET'[/i][/green][teal],[/teal][green][i]'[URL unfurl="true"]http://webserver:8080/recid='[/URL][/i][/green][teal]+[/teal]service[teal][[/teal]i[teal]][[/teal][purple]0[/purple][teal]],[/teal][b]false[/b][teal])[/teal]
    http[teal].[/teal][COLOR=darkgoldenrod]send[/color][teal]([/teal][b]null[/b][teal])[/teal]
    [b]if[/b] [teal]([/teal]http[teal].[/teal]readyState[teal]!=[/teal][purple]4[/purple] [teal]||[/teal] http[teal].[/teal]status[teal]!=[/teal][purple]200[/purple][teal])[/teal]
      service[teal][[/teal]i[teal]][[/teal][purple]1[/purple][teal]].[/teal]innerHTML[teal]=[/teal][green][i]'checking service '[/i][/green][teal]+[/teal]service[teal][[/teal]i[teal]][[/teal][purple]0[/purple][teal]]+[/teal][green][i]' FAILED'[/i][/green]
    [b]else[/b]
      service[teal][[/teal]i[teal]][[/teal][purple]1[/purple][teal]].[/teal]innerHTML[teal]=[/teal]http[teal].[/teal]responseText[teal].[/teal][COLOR=darkgoldenrod]replace[/color][teal]([/teal][fuchsia]/[\s\S]*Status=([^<>]+)[\s\S]*Name=([^<>]+)[\s\S]*/[/fuchsia][teal],[/teal][green][i]'$2 is $1'[/i][/green][teal])[/teal]
  [teal]}[/teal]
[teal]}[/teal]
The status list will appear in the document where you place this HTML code :
Code:
[b]<ul[/b] [maroon]id[/maroon][teal]=[/teal][green][i]"statuslist"[/i][/green][b]></ul>[/b]
If something is not clear, just ask.

Feherke.
 
Hi

Forgot the warning : for any AJAX solution to work, you will need your document ( the one containing the above code ) to be on the same site as the remotely accessed one ( [ignore][/ignore] ). Or Cross-Origin Resource Sharing has to be enabled on the remote server. ( But its browser support is not complete yet. )

Feherke.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top