Umlaute beim Parsen einer HTML Seite

vimar

Bekanntes Mitglied
Hallo, ich benutze eine Klasse die mir den Titel einer HTML Seite ausgibt. Leider kommt es dazu dass Wörter wie z.b. "Düsseldorf" so geschrieben werden "Düsseldorf".

Was kann ich tun? Danke für jede Hilfe, die Klasse poste ich mal:

Java:
import java.io.*;
import java.net.*;

/**
 * This small application takes an URL as argument, downloads that
 * web page and outputs its HTML title to the screen.
 * 
 * Refer to the main() method for how to read a local file instead.
 */
public class GetTitleFromURL
{
  private static String startTag = "<title>";
  private static String endTag = "</title>";
  private static int startTagLength = startTag.length();
  private String title ="";
  

  public String getTitle() {
	return title;
}


public GetTitleFromURL( URL theURL, String theStart, String theEnd )
  {
    BufferedReader bufReader;
    String line;
    boolean foundStartTag = false;
    boolean foundEndTag = false;
    int startIndex, endIndex;
    this.startTag = theStart;
    this.endTag = theEnd;
   // String title = "";
    
    try
    {
      //open file
      bufReader = new BufferedReader( new InputStreamReader(theURL.openStream()) );
      
      //read line by line
      while( (line = bufReader.readLine()) != null && !foundEndTag)
      {
        //System.out.println(line);
      
        //search for title start tag (convert to lower case before searhing)
        if( !foundStartTag && (startIndex = line.toLowerCase().indexOf(startTag)) != -1 )
        {
          foundStartTag = true;
        }
        else
        {
          //else copy from start of string
          startIndex = -startTagLength;
        }
        
        //search for title start tag (convert to lower case before searhing)
        if( foundStartTag && (endIndex = line.toLowerCase().indexOf(endTag)) != -1 )
        {
          foundEndTag = true;
        }
        else
        {
          //else copy to end of string
          endIndex = line.length();
        }
        
        //extract title field
        if( foundStartTag || foundEndTag )
        {
          //System.out.println("foundStartTag="+foundStartTag);
          //System.out.println("foundEndTag="+foundEndTag);
          //System.out.println("startIndex="+startIndex);
          //System.out.println("startTagLength="+startTagLength);
          //System.out.println("endIndex="+endIndex);
        
          title += line.substring( startIndex + startTagLength, endIndex );
        }
      }
      
      //close the file when finished
      bufReader.close();
      
      //output the title
      if( title.length() > 0 )
      {
       // System.out.println("Title: "+title);
      }
      else
      {
        System.out.println("No title found in document.");
      }
      
    }
    catch( IOException e )
    {
      System.out.println( "GetTitle.GetTitle - error opening or reading URL: " + e );
    }
  }
 
Zum einen musst du beim Lesen der Seite den richtigen Charset angeben.
z.B.
Java:
bufReader = new BufferedReader(new InputStreamReader(theURL.openStream(), "utf8"));
Zum anderen muss System.out auch auf das Charset der Konsole eingestellt werden (unter Windows wäre das "cp850").
Dazu schreibst du an den Anfang der main Methode einfach
Java:
System.setOut(new PrintStream(System.out, true, "cp850"));
 
vielen dank, klappt nun mit "utf8", wenn ich allerdings das hier zusätzlich mache

Code:
System.setOut(new PrintStream(System.out, true, "cp850"));
dann krieg ich anstatt Düsseldorf D?sseldorf.

also einfach nur der erste tipp für die leute die per suchfunktion hier sind:
Code:
 new InputStreamReader(theURL.openStream(), "utf8"));
 
vielen dank, klappt nun mit "utf8", wenn ich allerdings das hier zusätzlich mache

Code:
System.setOut(new PrintStream(System.out, true, "cp850"));
dann krieg ich anstatt Düsseldorf D?sseldorf.[/code]

cp850 ist ja auch nur für die Windows Konsole. Bei anderen Betriebssystemen oder wenn der Text in der Konsole der IDE ausgegeben wird gilt das nicht.
 

Neue Themen


Zurück
Oben