Java Reference
In-Depth Information
try {
Reader reader = new FileReader("XMLText.xml");
DocumentPreprocessor dp = new DocumentPreprocessor(
reader, DocumentPreprocessor.DocType.XML);
dp.setElementDelimiter("sentence");
for (List sentence : dp) {
System.out.println(sentence);
}
} catch (FileNotFoundException ex) {
// Handle exception
}
The output of this example is as follows:
[When, the, day, is, done, we, can, sleep, .]
[When, the, morning, comes, we, can, wake, .]
[After, that, who, knows, .]
A cleaner output is possible using a ListIterator , as shown here:
for (List sentence : dp) {
ListIterator list = sentence.listIterator();
while (list.hasNext()) {
System.out.print(list.next() + " ");
}
System.out.println();
}
Its output is the following:
When the day is done we can sleep .
When the morning comes we can wake .
After that who knows .
If we had not specified an element delimiter, then each word would have been displayed
like this:
[When]
[the]
[day]
Search WWH ::




Custom Search