Search in sources :

Example 41 with SAXParser

use of javax.xml.parsers.SAXParser in project tdi-studio-se by Talend.

the class ComplexSAXLooper method parse.

/**
     * Parse the XML file. Buffer the result in LoopEntry.
     * 
     * @param is InputStream
     */
public void parse(java.io.InputStream is, String charset) {
    this.charset = charset;
    Reader reader = null;
    try {
        DefaultHandler hd = null;
        SAXParser saxParser = SAXParserFactory.newInstance().newSAXParser();
        if (rootPath == null || rootPath.equals("")) {
            hd = newHandler();
        } else {
            hd = newHandler2();
        }
        saxParser.setProperty("http://xml.org/sax/properties/lexical-handler", hd);
        // routines.system.UnicodeReader.java is used to ignore the BOM of the source file.
        reader = new UnicodeReader(is, this.charset);
        org.xml.sax.InputSource inSource = new org.xml.sax.InputSource(reader);
        saxParser.parse(inSource, hd);
    } catch (ParserConfigurationException e) {
        e.printStackTrace();
    } catch (SAXException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    } finally {
        if (reader != null) {
            try {
                reader.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
Also used : UnicodeReader(org.talend.xml.sax.io.UnicodeReader) Reader(java.io.Reader) SAXParser(javax.xml.parsers.SAXParser) UnicodeReader(org.talend.xml.sax.io.UnicodeReader) ParserConfigurationException(javax.xml.parsers.ParserConfigurationException) IOException(java.io.IOException) DefaultHandler(org.xml.sax.helpers.DefaultHandler) SAXException(org.xml.sax.SAXException)

Example 42 with SAXParser

use of javax.xml.parsers.SAXParser in project tdi-studio-se by Talend.

the class Rfh2AreaParser method parse.

public RFH2Area parse(String stringToParse) {
    SAXParser parser;
    try {
        parser = SAXParserFactory.newInstance().newSAXParser();
    } catch (Exception e) {
        throw new RuntimeException("Failed to create XML parser, can not parse RFH2 areas", e);
    }
    SaxHandler handler = new SaxHandler();
    try {
        parser.parse(new InputSource(new StringReader(stringToParse)), handler);
    } catch (SAXException e) {
        throw new RuntimeException("Invalid RFH2 header", e);
    } catch (IOException e) {
        throw new RuntimeException("Invalid RFH2 header", e);
    }
    return handler.getArea();
}
Also used : InputSource(org.xml.sax.InputSource) StringReader(java.io.StringReader) SAXParser(javax.xml.parsers.SAXParser) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException) IOException(java.io.IOException) SAXException(org.xml.sax.SAXException)

Example 43 with SAXParser

use of javax.xml.parsers.SAXParser in project Synthese_2BIN by TheYoungSensei.

the class MainSAXB method main.

public static void main(String[] args) {
    try {
        File inputFile = new File("library.xml");
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();
        SAXB userHandler = new SAXB();
        saxParser.parse(inputFile, userHandler);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : SAXParser(javax.xml.parsers.SAXParser) File(java.io.File) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 44 with SAXParser

use of javax.xml.parsers.SAXParser in project Synthese_2BIN by TheYoungSensei.

the class MainSAXC method main.

public static void main(String[] args) {
    try {
        File inputFile = new File("course.xml");
        SAXParserFactory factory = SAXParserFactory.newInstance();
        SAXParser saxParser = factory.newSAXParser();
        SAXC userHandler = new SAXC();
        saxParser.parse(inputFile, userHandler);
    } catch (Exception e) {
        e.printStackTrace();
    }
}
Also used : SAXParser(javax.xml.parsers.SAXParser) File(java.io.File) SAXParserFactory(javax.xml.parsers.SAXParserFactory)

Example 45 with SAXParser

use of javax.xml.parsers.SAXParser in project wh-app-android by WhiteHouse.

the class FeedManager method updateFeedFromServer.

public static void updateFeedFromServer(String url, String title, String viewType) {
    final BehaviorSubject<Observable<List<FeedItem>>> subject;
    final OkHttpClient client = getClient();
    final Request request = getBaseRequest().url(url).get().build();
    if (sFeedItemsSubject.get(url) == null) {
        sFeedItemsSubject.put(url, BehaviorSubject.create());
    }
    subject = sFeedItemsSubject.get(url);
    subject.onNext(Observable.create((Subscriber<? super List<FeedItem>> op) -> {
        Response response;
        try {
            response = client.newCall(request).execute();
            if (response.code() >= 400) {
                op.onError(new IOException("Response code " + Integer.toString(response.code())));
            } else {
                SAXParser parser = SAXParserFactory.newInstance().newSAXParser();
                FeedHandler handler = new FeedHandler(title, viewType);
                parser.parse(response.body().byteStream(), handler);
                op.onNext(handler.getFeedItems());
            }
            op.onCompleted();
        } catch (Exception e) {
            op.onError(e);
        }
    }));
}
Also used : Response(com.squareup.okhttp.Response) OkHttpClient(com.squareup.okhttp.OkHttpClient) FeedItem(gov.whitehouse.data.model.FeedItem) FeedHandler(gov.whitehouse.core.FeedHandler) Request(com.squareup.okhttp.Request) SAXParser(javax.xml.parsers.SAXParser) IOException(java.io.IOException) Observable(rx.Observable) IOException(java.io.IOException) FileNotFoundException(java.io.FileNotFoundException)

Aggregations

SAXParser (javax.xml.parsers.SAXParser)235 SAXParserFactory (javax.xml.parsers.SAXParserFactory)142 SAXException (org.xml.sax.SAXException)112 InputSource (org.xml.sax.InputSource)95 IOException (java.io.IOException)80 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)71 DefaultHandler (org.xml.sax.helpers.DefaultHandler)37 XMLReader (org.xml.sax.XMLReader)36 File (java.io.File)35 ByteArrayInputStream (java.io.ByteArrayInputStream)28 StringReader (java.io.StringReader)27 InputStream (java.io.InputStream)24 Attributes (org.xml.sax.Attributes)22 SAXSource (javax.xml.transform.sax.SAXSource)17 SAXParseException (org.xml.sax.SAXParseException)17 ArrayList (java.util.ArrayList)13 JAXBContext (javax.xml.bind.JAXBContext)12 Unmarshaller (javax.xml.bind.Unmarshaller)12 FileNotFoundException (java.io.FileNotFoundException)10 ValidationEvent (javax.xml.bind.ValidationEvent)9