Example usage for javax.xml.parsers SAXParser parse

List of usage examples for javax.xml.parsers SAXParser parse

Introduction

In this page you can find the example usage for javax.xml.parsers SAXParser parse.

Prototype

public void parse(InputSource is, DefaultHandler dh) throws SAXException, IOException 

Source Link

Document

Parse the content given org.xml.sax.InputSource as XML using the specified org.xml.sax.helpers.DefaultHandler .

Usage

From source file:com.markwatson.linkeddata.DBpediaLookupClient.java

public DBpediaLookupClient(String query) throws Exception {
    this.query = query;
    HttpClient client = new HttpClient();

    String query2 = query.replaceAll(" ", "+");
    HttpMethod method = new GetMethod(
            "http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryString=" + query2);
    try {/*from ww w .j a v a  2  s. co m*/
        client.executeMethod(method);
        InputStream ins = method.getResponseBodyAsStream();
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser sax = factory.newSAXParser();
        sax.parse(ins, this);
    } catch (HttpException he) {
        System.err.println("Http error connecting to lookup.dbpedia.org");
    } catch (IOException ioe) {
        System.err.println("Unable to connect to lookup.dbpedia.org");
    }
    method.releaseConnection();
}

From source file:com.knowledgebooks.info_spiders.DBpediaLookupClient.java

public DBpediaLookupClient(String query) throws Exception {
    this.query = query;
    //System.out.println("\n query: " + query);
    HttpClient client = new HttpClient();

    String query2 = query.replaceAll(" ", "+"); // URLEncoder.encode(query, "utf-8");
    //System.out.println("\n query2: " + query2);
    HttpMethod method = new GetMethod(
            "http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryString=" + query2);
    try {//from ww  w. jav  a2  s  . c o m
        System.out.println("\n method: " + method.getURI());
        client.executeMethod(method);
        System.out.println(method);
        InputStream ins = method.getResponseBodyAsStream();
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser sax = factory.newSAXParser();
        sax.parse(ins, this);
    } catch (HttpException he) {
        System.err.println("Http error connecting to lookup.dbpedia.org");
    } catch (IOException ioe) {
        System.err.println("Unable to connect to lookup.dbpedia.org");
    }
    method.releaseConnection();
}

From source file:com.xhsoft.framework.common.init.ConfigLoad.java

/**
 * <p>Description:?xmlString?</p>
 * @param strFilePath   /*from   ww  w  .j a  v  a2s .  co m*/
 * @return String
 * @author wenzhi
 * @version 1.0
 */
public void parser(String strFilePath) {
    try {
        /**??*/
        InputStream stream = this.getClass().getResourceAsStream(strFilePath);
        if (stream == null) {
            log.warn(strFilePath + " not found");
        }
        /**SAXParser??*/
        SAXParserFactory spf = SAXParserFactory.newInstance();
        SAXParser saxParser = spf.newSAXParser();
        saxParser.parse(stream, this);
        /** ??*/
        this.loadConfig(this.configMap);
    } catch (Exception ex) {
        log.error(" " + strFilePath + " ! ?.");
        ex.printStackTrace();
    }
}

From source file:net.cpollet.jixture.fixtures.transformers.XmlFileFixtureTransformer.java

@Override
protected List<Object> parse(XmlFileFixture fixture) {
    InputStream inputStream = fixture.getInputStream();

    XmlFileToMappingsFixtureTransformerSAXHandler handler = createSaxHandler();
    SAXParser parser = createSaxParser();

    try {/*  w  w w.java2 s.co  m*/
        parser.parse(inputStream, handler);
    } catch (Exception e) {
        throw ExceptionUtils.wrapInRuntimeException(e);
    }

    return handler.getObjects();
}

From source file:com.comcast.cqs.test.stress.CqsStressTester.java

public static String deserialize(String serialized, String key) {
    javax.xml.parsers.SAXParserFactory fac = new org.apache.xerces.jaxp.SAXParserFactoryImpl();
    javax.xml.parsers.SAXParser saxParser;
    SaxHandler p = new SaxHandler();
    try {/*from  w ww  . j ava  2 s .  co  m*/
        saxParser = fac.newSAXParser();
        saxParser.parse(new ByteArrayInputStream(serialized.getBytes()), p);
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

    return p.getValueByKey(key);
}

From source file:com.qspin.qtaste.recorder.SpyInstaller.java

public SpyInstaller(String pXmlFilterDefinitionPath) {
    super();/*  www.  j ava 2  s .c o m*/
    mFilter = new ArrayList<RecorderFilter>();
    if (pXmlFilterDefinitionPath != null) {
        FilterXmlHandler gestionnaire = new FilterXmlHandler();
        try {
            SAXParserFactory fabrique = SAXParserFactory.newInstance();
            SAXParser parseur = fabrique.newSAXParser();
            parseur.parse(pXmlFilterDefinitionPath, gestionnaire);
            for (Filter f : gestionnaire.getDecodedFilters()) {
                mFilter.add(new RecorderFilter(f));
            }
        } catch (IOException pExc) {
            LOGGER.error(pExc);
        } catch (SAXException pExc) {
            LOGGER.error(pExc);
        } catch (ParserConfigurationException pExc) {
            LOGGER.error(pExc);
        }
    }
}

From source file:de.l3s.souza.search.DBpediaLookupClient.java

public DBpediaLookupClient(String query) throws Exception {
    this.query = query;
    //System.out.println("\n query: " + query);
    HttpClient client = new HttpClient();

    String query2 = query.replaceAll(" ", "+"); // URLEncoder.encode(query, "utf-8");
    //System.out.println("\n query2: " + query2);
    HttpMethod method = new GetMethod(
            "http://lookup.dbpedia.org/api/search.asmx/KeywordSearch?QueryString=" + query2);
    try {//from  w w  w.  j a  v  a2  s.  c  om
        //    System.out.println("\n method: " + method.getURI());
        client.executeMethod(method);
        //  System.out.println(method);

        InputStream ins = method.getResponseBodyAsStream();
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser sax = factory.newSAXParser();
        sax.parse(ins, this);
    } catch (HttpException he) {
        System.err.println("Http error connecting to lookup.dbpedia.org");
    } catch (IOException ioe) {
        System.err.println("Unable to connect to lookup.dbpedia.org");
    }
    method.releaseConnection();
}

From source file:com.comcast.cqs.test.stress.CqsStressTester.java

public static List<CQSMessage> deserializeMessage(String serializedMessage) {
    javax.xml.parsers.SAXParserFactory fac = new org.apache.xerces.jaxp.SAXParserFactoryImpl();
    javax.xml.parsers.SAXParser saxParser;
    SaxHandler p = new SaxHandler();
    try {//from   w w  w.j  av a2s  .c  o m
        saxParser = fac.newSAXParser();
        saxParser.parse(new ByteArrayInputStream(serializedMessage.getBytes()), p);
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (Exception e) {
        e.printStackTrace();
    }

    CQSMessage cqsMessage = null;

    try {
        if (p.getValueByKey("Body") == null) {
            return new ArrayList<CQSMessage>();
        }
        cqsMessage = new CQSMessage(p.getValueByKey("Body"), p.getAttributes());
        cqsMessage.setMessageId(p.getValueByKey("MessageId"));
        cqsMessage.setReceiptHandle(p.getValueByKey("ReceiptHandle"));
        cqsMessage.setMD5OfBody(p.getValueByKey("MD5OfBody"));
    } catch (NoSuchAlgorithmException e) {
        e.printStackTrace();
    } catch (UnsupportedEncodingException e) {
        e.printStackTrace();
    }
    return java.util.Arrays.asList(cqsMessage);
}

From source file:com.tupilabs.pbs.parser.NodeXmlParser.java

@Override
public List<Node> parse(String xml) throws ParseException {
    try {// ww  w.  j ava 2  s .c  o m
        final SAXParserFactory factory = SAXParserFactory.newInstance();
        final SAXParser saxParser = factory.newSAXParser();
        final NodeXmlHandler handler = new NodeXmlHandler();

        saxParser.parse(new CharSequenceInputStream(xml, Charset.defaultCharset()), handler);

        return handler.getNodes();
    } catch (IOException ioe) {
        throw new ParseException(ioe);
    } catch (SAXException e) {
        throw new ParseException(e);
    } catch (ParserConfigurationException e) {
        throw new ParseException(e);
    }
}

From source file:org.sansdemeure.zenindex.indexer.odt.TestHTMLConverter.java

@Test
public void test() throws ParserConfigurationException, SAXException, IOException {
    TestAppender testAppender = new TestAppender();
    File testDir = FileUtil.prepareEmptyDirectory(TestHTMLConverter.class);
    FileUtil.copyFromResources("docs/1992/Sandokai_with_2comments.odt", testDir, "Sandokai.odt");
    File odt = new File(testDir, "Sandokai.odt");
    try (ODTResource odtRessource = new ODTResource(odt)) {
        InputStream in = odtRessource.openContentXML();
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();
        OdtHTMLConverterHandler htmlConverter = new OdtHTMLConverterHandler();
        saxParser.parse(in, htmlConverter);
        testAppender.verify("paragraphs were created");
        testAppender.verify("spans were created");
        testAppender.verify("2 annotations were created");
        testAppender.verify("All paragraphs were closed");
        testAppender.verify("All spans were closed");
    }/* ww  w . j  a v  a2 s .c o m*/

}