Anche se è passato un po' di tempo ti ringrazio andbin per la risposta...
Adesso ho implementato questo metodo che riceve come argomento un file html e ne stampa tutti i tag o stringhe.
codice:
public void CodeHtml(File file) {
try {
BufferedReader buf = new BufferedReader(new FileReader(file));
StringBuilder temp = new StringBuilder();
char[] characters = new char[512];
while(buf.read(characters) >= 0)
temp.append(characters);
String codeHtml = temp.toString().replaceAll("\n", " ");
Pattern pat = Pattern.compile("((</?\\w+>)|[^<]+)");
Matcher mat = pat.matcher(codeHtml);
while(mat.find())
System.out.println("piece of code: " + mat.group());
} catch(Exception e) {
System.err.println(e);
}
}
Il file che ho usato come test è il seguente:
codice:
<html>
<head>
</head>
<body>
Ciao Mondo </p>
</body>
</html>
Ed infine l'outup è il seguente:
codice:
piece of code: <html>
piece of code:
piece of code: <head>
piece of code:
piece of code: </head>
piece of code:
piece of code: <body>
piece of code:
piece of code:
piece of code: Ciao Marco
piece of code: </p>
piece of code:
piece of code: </body>
piece of code:
piece of code: </html>
piece of code:
Dall'output che ottengo sembrerebbe che il comando mat.group() ritorni con periodicità una stringa vuota. Vorrei capire come mai succede questo e naturalmente come impedirlo...