| 1 16 package org.outerj.daisy.textextraction.impl; 17 18 import org.xml.sax.helpers.DefaultHandler ; 19 import org.xml.sax.SAXException ; 20 21 import javax.xml.parsers.SAXParserFactory ; 22 import javax.xml.parsers.SAXParser ; 23 import java.io.InputStream ; 24 25 30 public class XmlTextExtractor implements MimetypeTextExtractor { 31 public String getText(InputStream is) throws Exception { 32 SAXParserFactory factory = SAXParserFactory.newInstance(); 33 SAXParser parser = factory.newSAXParser(); 34 MyHandler handler = new MyHandler(); 35 parser.parse(is, handler); 36 return handler.getText(); 37 } 38 39 private static class MyHandler extends DefaultHandler { 40 StringBuffer text = new StringBuffer (); 41 42 public void characters(char ch[], int start, int length) throws SAXException { 43 text.append(ch, start, length); 44 } 45 46 public String getText() { 47 return text.toString(); 48 } 49 } 50 } 51 | Popular Tags |