File: TitleSearch.java

package info (click to toggle)
xom 1.3.9-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 15,712 kB
  • sloc: xml: 68,724; java: 48,828; makefile: 17
file content (41 lines) | stat: -rwxr-xr-x 1,094 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
import nu.xom.*;
import java.io.IOException;

public class TitleSearch {

  public static void main(String[] args) {
   
   if (args.length == 0) {
     System.err.println("Usage: java TitleSearch url");
     return;
   }      

   String pageURL = args[0];
   
   Builder builder = new Builder();
   try {
     Document doc = builder.build(pageURL);
     Element html = doc.getRootElement();
     Element head = html.getFirstChildElement("head");
     if (head == null) {
       head = html.getFirstChildElement("head", "http://www.w3.org/1999/xhtml");
     }
     Element title = head.getFirstChildElement("title");  
     if (title == null) {
       title = head.getFirstChildElement("title", "http://www.w3.org/1999/xhtml"); 
     }
     System.out.println(title.getValue());
   }
   catch (NullPointerException ex) {
     System.err.println(pageURL + " does not have a title.");     
   }
   catch (ParsingException ex) {
     System.err.println(pageURL + " is malformed.");     
   }
   catch (IOException ex) {
     System.err.println("Could not read " + pageURL); 
   }  
    
  }
  
}