2015-05-27 3 views
1

Я ищу, чтобы написать программу, которая читает из html-файла и копирует содержимое, но игнорирует теги html без использования replaceAll. Также удаление дескрипторов html должно выполняться другим способом. Файл выглядит следующим образом:Скопируйте содержимое файла при игнорировании символов между < and > в Java

<html> 
<head> 
<title>My web page</title> 
</head> 
<body> 
<p>There are many pictures of my cat here, 
as well as my <b>very cool</b> blog page, 
which contains <font color="red">awesome 
stuff about my trip to Vegas.</p> 


Here's my cat now:<img src="cat.jpg"> 
</body> 
</html> 

И я хотел бы свою программу, чтобы отобразить следующее:

My web page 


There are many pictures of my cat here, 
as well as my very cool blog page, 
which contains awesome 
stuff about my trip to Vegas. 


Here's my cat now: 

ответ

0
public static void main(String[] args) { 
    String html = " <html>\n" 
      + " <head>\n" 
      + " <title>My web page</title>\n" 
      + " </head>\n" 
      + " <body>\n" 
      + " <p>There are many pictures of my cat here,\n" 
      + " as well as my <b>very cool</b> blog page,\n" 
      + " which contains <font color=\"red\">awesome\n" 
      + " stuff about my trip to Vegas.</p>\n" 
      + "\n" 
      + "\n" 
      + " Here's my cat now:<img src=\"cat.jpg\">\n" 
      + " </body>\n" 
      + " </html>"; 

    boolean inTag = false; 
    StringBuilder finalString = new StringBuilder(); 

    int length = html.length(); 
    for (int i = 0; i < length; i++) { 

     char c = html.charAt(i); 

     if ('<' == c) { 
      inTag = true; 
     } else if ('>' == c) { 
      inTag = false; 
     } else if (!inTag) { 
      finalString.append(c); 
     } 

    } 

    System.out.print(finalString); 

} 
+1

Вместо того, чтобы просто дать код, вы должны объяснить, что вы делаете. – Keelan