Example usage for org.xml.sax XMLReader parse

List of usage examples for org.xml.sax XMLReader parse

Introduction

In this page you can find the example usage for org.xml.sax XMLReader parse.

Prototype

public void parse(String systemId) throws IOException, SAXException;

Source Link

Document

Parse an XML document from a system identifier (URI).

Usage

From source file:net.eledge.android.europeana.tools.RssReader.java

public static List<BlogArticle> readFeed(String url, DateTime lastViewed) {
    InputStream is = null;/* w  ww.j  a va 2 s. co  m*/
    try {
        HttpGet request = new HttpGet(url);
        AndroidHttpClient.modifyRequestToAcceptGzipResponse(request);
        HttpResponse response = new DefaultHttpClient().execute(request);
        is = AndroidHttpClient.getUngzippedContent(response.getEntity());

        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser sp = spf.newSAXParser();
        XMLReader xr = sp.getXMLReader();

        RssFeedHandler rh = new RssFeedHandler(lastViewed);

        xr.setContentHandler(rh);
        xr.parse(new InputSource(is));

        return rh.articles;
    } catch (IOException | SAXException | ParserConfigurationException e) {
        Log.e("RssReader", e.getMessage(), e);
    } finally {
        IOUtils.closeQuietly(is);
    }

    return null;
}

From source file:Main.java

public static void parse(final XMLReader xmlReader, final URL url) throws IOException, SAXException {
    final InputStream inputStream = url.openStream();
    try {/*w  w w.  j av  a 2  s.com*/
        final InputSource inputSource = new InputSource(inputStream);
        xmlReader.parse(inputSource);
    } finally {
        inputStream.close();
    }

}

From source file:net.sf.janos.model.xml.ResultParser.java

/**
 * @param xml/*  w ww  . j  a v a2 s . co m*/
 * @return a list of Entrys from the given xml string.
 * @throws IOException
 * @throws SAXException
 */
public static List<Entry> getEntriesFromStringResult(String xml) throws SAXException {
    XMLReader reader = XMLReaderFactory.createXMLReader();
    EntryHandler handler = new EntryHandler();
    reader.setContentHandler(handler);
    try {
        reader.parse(new InputSource(new StringReader(xml)));
    } catch (IOException e) {
        // This should never happen - we're not performing I/O!
        LOG.error("Could not parse entries: ", e);
    }
    return handler.getArtists();
}

From source file:net.sf.janos.model.xml.ResultParser.java

public static TrackMetaData parseTrackMetaData(String xml) throws SAXException {
    XMLReader reader = XMLReaderFactory.createXMLReader();
    TrackMetaDataHandler handler = new TrackMetaDataHandler();
    reader.setContentHandler(handler);//from w  w  w  .  j  av  a  2  s  . c o  m
    try {
        reader.parse(new InputSource(new StringReader(xml)));
    } catch (IOException e) {
        // This should never happen - we're not performing I/O!
        LOG.error("Could not parse AV Transport Event: ", e);
    }
    return handler.getMetaData();
}

From source file:net.sf.janos.model.xml.ResultParser.java

/**
 * @param controller/*  w w  w  .j a va  2 s .com*/
 * @param xml
 * @return zone group state from the given xml
 * @throws IOException
 * @throws SAXException
 */
public static ZoneGroupState getGroupStateFromResult(String xml) throws SAXException {
    XMLReader reader = XMLReaderFactory.createXMLReader();
    ZoneGroupStateHandler handler = new ZoneGroupStateHandler();
    reader.setContentHandler(handler);
    try {
        reader.parse(new InputSource(new StringReader(xml)));
    } catch (IOException e) {
        // This should never happen - we're not performing I/O!
        LOG.error("Could not parse group state: ", e);
    }

    return new ZoneGroupState(handler.getGroups());

}

From source file:net.sf.janos.model.xml.ResultParser.java

public static Map<AVTransportEventHandler.AVTransportEventType, String> parseAVTransportEvent(String xml)
        throws SAXException {
    XMLReader reader = XMLReaderFactory.createXMLReader();
    AVTransportEventHandler handler = new AVTransportEventHandler();
    reader.setContentHandler(handler);/*from w w w.ja v a2  s  .  com*/
    try {
        reader.parse(new InputSource(new StringReader(xml)));
    } catch (IOException e) {
        // This should never happen - we're not performing I/O!
        LOG.error("Could not parse AV Transport Event: ", e);
    }
    return handler.getChanges();
}

From source file:net.sf.janos.model.xml.ResultParser.java

public static Map<RenderingControlEventHandler.RenderingControlEventType, String> parseRenderingControlEvent(
        String xml) throws SAXException {
    XMLReader reader = XMLReaderFactory.createXMLReader();
    RenderingControlEventHandler handler = new RenderingControlEventHandler();
    reader.setContentHandler(handler);/*from   w ww .  jav a 2  s.com*/
    try {
        reader.parse(new InputSource(new StringReader(xml)));
    } catch (IOException e) {
        // This should never happen - we're not performing I/O!
        LOG.error("Could not parse Rendering Control event: ", e);
    }
    return handler.getChanges();
}

From source file:eu.project.ttc.test.unit.TestUtil.java

public static String getTeiTxt(String filename)
        throws ParserConfigurationException, SAXException, IOException, FileNotFoundException {
    SAXParserFactory spf = SAXParserFactory.newInstance();
    spf.setNamespaceAware(true);/*from   w  w  w  . ja  v  a2s  .  c o m*/
    spf.setValidating(false);
    spf.setFeature("http://xml.org/sax/features/namespaces", true);
    spf.setFeature("http://xml.org/sax/features/validation", false);
    spf.setFeature("http://apache.org/xml/features/nonvalidating/load-dtd-grammar", false);
    spf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false);

    SAXParser saxParser = spf.newSAXParser();
    XMLReader xmlReader = saxParser.getXMLReader();
    TeiToTxtSaxHandler handler = new TeiToTxtSaxHandler();
    xmlReader.setContentHandler(handler);
    xmlReader.parse(new InputSource(TestUtil.getInputStream(filename)));
    String text = handler.getText();
    return text;
}

From source file:Main.java

/**
 * Parses the given {@link File} with the specified {@link XMLReader}.
 * //from w ww .ja  va  2  s.  c  o  m
 * <p>This method assumes the XML is in 'UTF-8', it will not sniff the XML to
 * determine the encoding to use.
 * 
 * @param xmlreader The XML reader to use.
 * @param file      The file to parse
 * 
 * @throws FileNotFoundException If the file does not exists
 * @throws SAXException          Should an SAX exception occur
 * @throws IOException           Should an I/O exception occur
 */
public static void parse(XMLReader xmlreader, File file)
        throws FileNotFoundException, SAXException, IOException {
    // create the input source
    InputStream bin = new BufferedInputStream(new FileInputStream(file));
    Reader reader = new InputStreamReader(bin, "utf-8");
    InputSource source = new InputSource(reader);
    // parse the file
    xmlreader.parse(source);
    reader.close();
}

From source file:org.apache.clerezza.commons.rdf.impl.sparql.SparqlResultParser.java

static Object parse(InputStream in) throws IOException {
    try {/*  w  w w.ja v  a2  s . com*/
        SAXParserFactory spf = SAXParserFactory.newInstance();
        spf.setNamespaceAware(true);
        SAXParser saxParser = spf.newSAXParser();
        XMLReader xmlReader = saxParser.getXMLReader();
        final SparqlsResultsHandler sparqlsResultsHandler = new SparqlsResultsHandler();
        xmlReader.setContentHandler(sparqlsResultsHandler);
        xmlReader.parse(new InputSource(in));
        return sparqlsResultsHandler.getResults();
    } catch (ParserConfigurationException | SAXException ex) {
        throw new RuntimeException(ex);
    }
}