J'essaie d'analyser d'un simple fragment de HTML avec NekoHTML:fragment NekoHTML SAX analyse syntaxique
<h1>This is a basic test</h1>
Pour ce faire, je me suis fixé un specific Neko feature ne pas avoir de HTML, HEAD ou balise BODY appelant startElement (..) rappeler. Malheureusement, cela ne fonctionne pas pour moi .. J'ai certainement raté quelque chose mais je ne sais pas ce que ce serait.
Voici un code très simple à reproduire mon problème:
public static class MyContentHandler implements ContentHandler {
public void characters(char[] ch, int start, int length) throws SAXException {
String text = String.valueOf(ch, start, length);
System.out.println(text);
}
public void startElement(String nameSpaceURI, String localName, String rawName, Attributes attributes) throws SAXException {
System.out.println(rawName);
}
public void endElement(String nameSpaceURI, String localName, String rawName) throws SAXException {
System.out.println("end " + localName);
}
}
Et le principal() pour lancer un test:
public static void main(String[] args) throws SAXException, IOException {
SAXParser saxReader = new SAXParser();
// set the feature like explained in documentation : http://nekohtml.sourceforge.net/faq.html#fragments
saxReader.setFeature("http://cyberneko.org/html/features/balance-tags/document-fragment", true);
saxReader.setContentHandler(new MyContentHandler());
saxReader.parse(new InputSource(new StringInputStream("<h1>This is a basic test</h1>")));
}
La sortie correspondante:
HTML
HEAD
end HEAD
BODY
H1
This is a basic test
end H1
end BODY
end HTML
alors que je m'attendais à
H1
This is a basic test
end H1
Une idée?
Si vous définissez la fonctionnalité sur false, obtenez-vous exactement la même sortie? –
Oui, exactement la même chose :-( – Gael