Java Sax Parser Example Inputstream

Best Java code snippets using javax.xml.parsers.SAXParser.parse (Showing top 20 results out of 7,344)

Refine searchRefine arrow

                                                                                                                          SAXParserFactory factory = SAXParserFactory.                                                        newInstance                                                        ();                                                                                                                                                    factory.setValidating(                                                        true                                                        );                                                                                                                                                                                                          try                                                                              {                                                                                                                                                    SAXParser saxParser = factory.                                                        newSAXParser                                                        ();                                                                                                                                                    File file =                                                                            new                                                                              File(                                                        "test.xml"                                                        );                                                                                                                                                    saxParser.                                                        parse                                                        (file,                                                                            new                                                                              ElementHandler());                                                                                                                                                                                                        }                                                                                                                                                                    catch                                                        (ParserConfigurationException e1) {                                                                                                          }                                                                                                                                                                    catch                                                        (SAXException e1) {                                                                                                          }                                                                                                                                                                    catch                                                        (IOException e) {                                                                                                          }                              
                                                                                                                                public                                                                                                                                    void                                                                              load(InputSource inputSource)                                                                                                                                                                                                          throws                                                                              ParserConfigurationException, SAXException, IOException {                                                                                                                                                    SAXParser saxParser = saxParserFactory.                                                        newSAXParser                                                        ();                                                                                                                                                    saxParser.                                                        parse                                                        (inputSource,                                                                            new                                                                              DefaultHandler() {                                                                                                          @Override                                                                                                                                                                    public                                                                                                                                    void                                                                              startElement(String uri, String localName, String qName, Attributes attributes)                                                                            throws                                                                              SAXException {                                                                                                                                                                                                          if                                                                              (localName.equals(                                                        "factory"                                                        )) {                                                                                                                                                    String className = attributes.getValue(                                                        "class"                                                        );                                                                                                                                                                                                          try                                                                              {                                                                                                          addClass(className);                                                                                                              }                                                                            catch                                                                              (ClassNotFoundException e) {                                                                                                                                                                                                          throw                                                                                                                                    new                                                                              SAXException(                                                        "Cannot find Matcher class : "                                                                              + className);                                                                                                          }                                                                    }                                                                    }                                                                    });                                                  }                                                                                
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        public                                                                                                                                    void                                                                              parse(InputStream is, DefaultHandler dh)                                                                                                                                                                                                          throws                                                                              SAXException, IOException {                                                                                                                                                                                                          if                                                                              (is == null) {                                                                                                                                                                                                          throw                                                                                                                                    new                                                                              IllegalArgumentException(                                                        "InputStream cannot be null"                                                        );                                                                                                          }                                                                                                                                                                InputSource input =                                                                            new                                                                                                                                    InputSource                                                        (is);                                                                                                                                                                                                          this                                                        .                                                        parse                                                        (input, dh);                                                                                        }                                                                                
                                                                                                                                                                                                      try                                                                                                                                                {                                                                    HttpEntity entity = response.getEntity();                                                                                                                                                                    final                                                                              InputStream in = entity.getContent();                                                                                                                                                                                                          final                                                                              SAXParser parser = SAXParserFactory.                                                        newInstance                                                        ().                                                        newSAXParser                                                        ();                                                                                                                                                                                                          final                                                                              XmlHandler handler =                                                                            new                                                                              XmlHandler();                                                                                                                                                    Reader reader =                                                                            new                                                                              InputStreamReader(in,                                                                            "UTF-8"                                                        );                                                                                                                                                    InputSource is =                                                                            new                                                                                                                                    InputSource                                                        (reader);                                                                                                                                                    is.setEncoding(                                                        "UTF-8"                                                        );                                                                                                                                                    parser.                                                        parse                                                        (is, handler);                                                                                                                                                                                                                                                                                                                    }                                                                                        catch                                                                              (final Exception e)                                                                                        {                                                                                                              Log.e(                                                        "ParseError"                                                        ,                                                                            "Error parsing xml"                                                        , e);                                                                                        }                              
                                                                                                                          InputSource input =                                                                            new                                                                                                                                    InputSource                                                        (file.toURI().toASCIIString());                                                                                        input.setByteStream(in);                                                                                        JAXP.                                                        newSAXParser                                                        ().                                                        parse                                                        (input,                                                        new                                                                              DefaultHandler() {                                                                                                                                                                                                          private                                                                              Locator loc;                                                                                                          @Override                                                                                
                                                                                                                                public                                                                              Set<String> parseContents(InputSource contents)                                                                            throws                                                                              IOException, ParserConfigurationException, SAXException {                                                                                                          namespaces.clear();                                                                                                                                                                                                                                                                                                                                                                                                if                                                                              (factory == null) {                                                                                                                                                    factory = SAXParserFactory.                                                        newInstance                                                        ();                                                                                                          }                                                                                                                                                                    if                                                                              (factory != null) {                                                                                                          SAXParser parser = createParser(factory);                                                                                                                                                                                                                                                                                                                                          contents.setSystemId(                                                        "/"                                                        );                                                                                                                                                                                                                                                  parser.                                                        parse                                                        (contents,                                                                            this                                                        );                                                                                                          }                                                                                                                                                                    return                                                                              namespaces;                                                                                        }                                                                                
                                                                                                                                public                                                                              Parse execute() {                                                                                                                                                                                                          try                                                                              {                                                                                                          InputStream inputStream = streamSource.getInputStream();                                                                                                                      parser.getSaxParserFactory().setFeature(XXE_PROCESSING, enableXxeProcessing);                                                                                                                                                                                                                      if                                                                              (schemaResource == null) {                                                                                                                                                                                                                                                  parser.getSaxParserFactory().                                                        setNamespaceAware                                                        (                                                        false                                                        );                                                                                                                                                    parser.getSaxParserFactory().setValidating(                                                        false                                                        );                                                                                                          }                                                                                                                      SAXParser saxParser = parser.getSaxParser();                                                                                                                                                                    if                                                                              (schemaResource != null) {                                                                                                          saxParser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);                                                                    saxParser.setProperty(JAXP_SCHEMA_SOURCE, schemaResource);                                                                    }                                                                                                              saxParser.                                                        parse                                                        (inputStream,                                                                            new                                                                              ParseHandler(                                                        this                                                        ));                                                                                                                                                            }                                                                                                                                                                    catch                                                                              (Exception e) {                                                                                                                                                                                                          throw                                                                              LOG.parsingFailureException(name, e);                                                                                                          }                                                                                                                                                                                                                      return                                                                                                                                    this                                                        ;                                                                                        }                                                                                
                                                                                                                                private                                                                                                                                    void                                                                              validateInternal(String xml, String dtdPath, String docType)                                                                            throws                                                                              SAXException, IOException, ParserConfigurationException {                                                                                                                                                    SAXParserFactory factory = SAXParserFactory.                                                        newInstance                                                        ();                                                                                                                                                    factory.setValidating(                                                        true                                                        );                                                                                                                                                    SAXParser saxParser = factory.                                                        newSAXParser                                                        ();                                                                                                                                                                                                                                                                                                                                                                                String cleanXml = xml.replaceAll(                                                        "<!DOCTYPE.+>"                                                        ,                                                                            ""                                                        );                                                                                                                                                    String decl =                                                                            "<?xml version=\"1.0\""                                                        ;                                                                                                                                                    String endDecl =                                                                            "?>"                                                        ;                                                                                                                                                    URL dtdUrl =                                                                            this                                                        .getClass().getResource(dtdPath);                                                                                                                                                                                                          if                                                                              (dtdUrl == null) {                                                                                                                                                                                                          throw                                                                                                                                    new                                                                              RuntimeException(                                                        "DTD not found in classpath: "                                                                              + dtdPath);                                                                                                          }                                                                                                              String dtd =                                                                            "<!DOCTYPE "                                                                              + docType +                                                                            " PUBLIC \"-//W3C//DTD Rules 0.1//EN\" \""                                                                              + dtdUrl +                                                                            "\">"                                                        ;                                                                                                                                                                                                          int                                                                              pos = cleanXml.indexOf(decl);                                                                                                                                                                                                          int                                                                              endPos = cleanXml.indexOf(endDecl);                                                                                                                                                                                                          if                                                                              (pos == -                                                        1                                                        ) {                                                                                                                                                                                                          throw                                                                                                                                    new                                                                              IOException(                                                        "No XML declaration found in '"                                                                              + cleanXml.substring(                                                        0                                                        , Math.min(                                                        100                                                        , cleanXml.length())) +                                                                            "...'"                                                        );                                                                                                          }                                                                                                              String newXML = cleanXml.substring(                                                        0                                                        , endPos+endDecl.length()) +                                                                            "\r\n"                                                                              + dtd + cleanXml.substring(endPos+endDecl.length());                                                                                                                                                    InputSource is =                                                                            new                                                                                                                                    InputSource                                                        (                                                        new                                                                              StringReader(newXML));                                                                                                                                                    saxParser.                                                        parse                                                        (is,                                                                            new                                                                              ErrorHandler());                                                                                        }                                                                                
                                                                                                                                                                                                                                                                                                                                        public                                                                              XmlInputArchive(InputStream in)                                                                                                                                                                                      throws                                                                              ParserConfigurationException, SAXException, IOException {                                                                                                                                                    valList =                                                                            new                                                                              ArrayList<Value>();                                                                                                                                                    DefaultHandler handler =                                                                            new                                                                              XMLParser(valList);                                                                                                                                                    SAXParserFactory factory = SAXParserFactory.                                                        newInstance                                                        ();                                                                                                          factory.setFeature(javax.xml.XMLConstants.FEATURE_SECURE_PROCESSING, Boolean.TRUE);                                                                                                              factory.setFeature(                                                        "http://apache.org/xml/features/disallow-doctype-decl"                                                        ,                                                                            true                                                        );                                                                                                                                                    SAXParser parser = factory.                                                        newSAXParser                                                        ();                                                                                                                                                    parser.                                                        parse                                                        (in, handler);                                                                                                          vLen = valList.size();                                                                                                              vIdx =                                                                            0                                                        ;                                                                                        }                                                                                
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        public                                                                                                                                    void                                                                              parse(String uri, DefaultHandler dh)                                                                                                                                                                                                          throws                                                                              SAXException, IOException {                                                                                                                                                                                                          if                                                                              (uri == null) {                                                                                                                                                                                                          throw                                                                                                                                    new                                                                              IllegalArgumentException(                                                        "uri cannot be null"                                                        );                                                                                                          }                                                                                                                                                                InputSource input =                                                                            new                                                                                                                                    InputSource                                                        (uri);                                                                                                                                                                                                          this                                                        .                                                        parse                                                        (input, dh);                                                                                        }                                                                                
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                          @Override                                                                                                                                                public                                                                              ParseResult parse(RSyntaxDocument doc, String style) {                                                                                                                                                            result.clearNotices();                                                                    Element root = doc.getDefaultRootElement();                                                                                                              result.setParsedLines(                                                        0                                                        , root.getElementCount()-                                                        1                                                        );                                                                                                                                                                                                                                                            if                                                                              (spf==null || doc.getLength()==                                                        0                                                        ) {                                                                                                                                                                                                          return                                                                              result;                                                                                                          }                                                                                                                                                                                                                      try                                                                              {                                                                                                                                                    SAXParser sp = spf.                                                        newSAXParser                                                        ();                                                                                                                                                    Handler handler =                                                                            new                                                                              Handler(doc);                                                                                                                                                    DocumentReader r =                                                                            new                                                                              DocumentReader(doc);                                                                                                                                                    InputSource input =                                                                            new                                                                                                                                    InputSource                                                        (r);                                                                                                                                                    sp.                                                        parse                                                        (input, handler);                                                                                                          r.close();                                                                                                              }                                                                            catch                                                                              (SAXParseException spe) {                                                                                                                                                                                                                                                                                                                                                                                }                                                                            catch                                                                              (Exception e) {                                                                                                                                                                                                                                                                                                                                                                                result.addNotice(                                                        new                                                                              DefaultParserNotice(                                                        this                                                        ,                                                                                                                                                                                                          "Error parsing XML: "                                                                              + e.getMessage(),                                                                            0                                                        , -                                                        1                                                        , -                                                        1                                                        ));                                                                                                          }                                                                                                                                                                                                                      return                                                                              result;                                                                                                                                          }                                                                                
                                                                                                                                public                                                                                                                                    void                                                                              parse( RepositoryElementReadListener repositoryElementReadListener )                                                                            throws                                                                              Exception {                                                                                                                                                                                                          this                                                        .repositoryElementReadListener = repositoryElementReadListener;                                                                                                                                                            SAXParserFactory factory = XMLParserFactoryProducer.createSecureSAXParserFactory();                                                                                                                                                                    this                                                        .saxParser = factory.                                                        newSAXParser                                                        ();                                                                                                                                                                                                          this                                                        .saxParser.                                                        parse                                                        (                                                                            new                                                                              File( filename ),                                                                            this                                                                              );                                                                                        }                                                                                
                                                                                                                                public                                                                              Parse execute() {                                                                                                                                                                                                          try                                                                              {                                                                                                          InputStream inputStream = streamSource.getInputStream();                                                                                                                      parser.getSaxParserFactory().setFeature(XXE_PROCESSING, enableXxeProcessing);                                                                                                                                                                                                                      if                                                                              (schemaResource == null) {                                                                                                                                                                                                                                                  parser.getSaxParserFactory().                                                        setNamespaceAware                                                        (                                                        false                                                        );                                                                                                                                                    parser.getSaxParserFactory().setValidating(                                                        false                                                        );                                                                                                          }                                                                                                                      SAXParser saxParser = parser.getSaxParser();                                                                                                                                                                    if                                                                              (schemaResource != null) {                                                                                                          saxParser.setProperty(JAXP_SCHEMA_LANGUAGE, W3C_XML_SCHEMA);                                                                    saxParser.setProperty(JAXP_SCHEMA_SOURCE, schemaResource);                                                                    }                                                                                                              saxParser.                                                        parse                                                        (inputStream,                                                                            new                                                                              ParseHandler(                                                        this                                                        ));                                                                                                                                                            }                                                                                                                                                                    catch                                                                              (Exception e) {                                                                                                                                                                                                          throw                                                                              LOG.parsingFailureException(name, e);                                                                                                          }                                                                                                                                                                                                                      return                                                                                                                                    this                                                        ;                                                                                        }                                                                                
                                                                                                                                                    public                                                                              DocumentData parseHTMLDocument(String document)                                                                            throws                                                                              HTMLParseException {                                                                                                                                                                                                          try                                                                              {                                                                                                                                                    SAXParserFactory saxParserFactory = SAXParserFactory.                                                        newInstance                                                        ();                                                                                                                                                    saxParserFactory.                                                        setNamespaceAware                                                        (                                                        false                                                        );                                                                                                                                                    saxParserFactory.setValidating(                                                        false                                                        );                                                                                                                                                    saxParserFactory.setXIncludeAware(                                                        false                                                        );                                                                                                                                                    SAXParser saxParser = saxParserFactory.                                                        newSAXParser                                                        ();                                                                                                                                                    HTMLDocumentHandler dh =                                                                            new                                                                              HTMLDocumentHandler(htmlDocumentBuilderFactory);                                                                                                                                                    saxParser.                                                        parse                                                        (                                                        new                                                                              ByteArrayInputStream(document.getBytes(                                                        "UTF-8"                                                        )), dh);                                                                                                                                                                                                          return                                                                              dh.getDocument();                                                                                                                                                    }                                                                            catch                                                                              (ParserConfigurationException | SAXException | IOException exp) {                                                                                                                                                                                                          throw                                                                                                                                    new                                                                              HTMLParseException(exp);                                                                                                          }                                                                    }                                                  }                              
                                                                                                                                                                                  final                                                                              Map<String,VersionNumber> requestedPlugins =                                                                            new                                                                              TreeMap<String,VersionNumber>();                                                                                                                                                                                      try                                                                              {                                                                                                                                                    SAXParserFactory.                                                        newInstance                                                        ().                                                        newSAXParser                                                        ().                                                        parse                                                        (configXml,                                                                            new                                                                              DefaultHandler() {                                                                                                                                                    @Override                                                                            public                                                                                                                                    void                                                                              startElement(String uri, String localName, String qName, Attributes attributes)                                                                            throws                                                                              SAXException {                                                                                                                                                    String plugin = attributes.getValue(                                                        "plugin"                                                        );                                                                                                                      
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        public                                                                                                                                    void                                                                              parse(InputStream is, HandlerBase hb)                                                                                                                                                                                                          throws                                                                              SAXException, IOException {                                                                                                                                                                                                          if                                                                              (is == null) {                                                                                                                                                                                                          throw                                                                                                                                    new                                                                              IllegalArgumentException(                                                        "InputStream cannot be null"                                                        );                                                                                                          }                                                                                                                                                                InputSource input =                                                                            new                                                                                                                                    InputSource                                                        (is);                                                                                                                                                                                                          this                                                        .                                                        parse                                                        (input, hb);                                                                                        }                                                                                
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        public                                                                                                                                    static                                                                                                                                    boolean                                                                              isXMLWellFormed( InputStream is )                                                                            throws                                                                              KettleException {                                                                                                                                                                                                          boolean                                                                              retval =                                                                            false                                                        ;                                                                                                                                                                                                          try                                                                              {                                                                                                          SAXParserFactory factory = XMLParserFactoryProducer.createSecureSAXParserFactory();                                                                                                              XMLTreeHandler handler =                                                                            new                                                                              XMLTreeHandler();                                                                                                                                                                                                                                                                                                                                                                                                                                  SAXParser saxParser = factory.                                                        newSAXParser                                                        ();                                                                                                                                                    saxParser.                                                        parse                                                        ( is, handler );                                                                                                                                                    retval =                                                                            true                                                        ;                                                                                                                                                    }                                                                            catch                                                                              ( Exception e ) {                                                                                                                                                                                                          throw                                                                                                                                    new                                                                              KettleException( e );                                                                                                          }                                                                                                                                                                    return                                                                              retval;                                                                                        }                                                                                
                                                                                                      String targetNamespace,                                                                    String schemaLocation) {                                                                                        InputSource in =                                                                            new                                                                                                                                    InputSource                                                        (xml);                                                                                                                                                                                                      SAXParserFactory sf = SAXParserFactory.                                                        newInstance                                                        ();                                                                                                                                                    sf.                                                        setNamespaceAware                                                        (                                                        true                                                        );                                                                                                                                                    sf.setValidating(                                                        true                                                        );                                                                                                                                                    SAXParser parser = sf.                                                        newSAXParser                                                        ();                                                                                                          parser.setProperty(                                                                                                                                                                    "http://java.sun.com/xml/jaxp/properties/schemaLanguage"                                                        ,                                                                                                                                                                                                      parser.                                                        parse                                                        (in, errorHandler);                                                                                                                      
                                                                                                                                public                                                                                                                                    void                                                                              parse(File wordpressXml) {                                                                                                                                                                                                          try                                                                              {                                                                                                                                                    SAXParserFactory factory = SAXParserFactory.                                                        newInstance                                                        ();                                                                                                                                                    SAXParser parser = factory.                                                        newSAXParser                                                        ();                                                                                                                                                    parser.                                                        parse                                                        (wordpressXml,                                                                            this                                                        );                                                                                                                                                    }                                                                            catch                                                                              (Exception e) {                                                                                                                                                    log.warn(                                                        "ConfigParser parser exception"                                                        , e);                                                                                                          }                                                                                                    }                                                                                
                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                        public                                                                                                                                    void                                                                              parse(String uri, HandlerBase hb)                                                                                                                                                                                                          throws                                                                              SAXException, IOException {                                                                                                                                                                                                          if                                                                              (uri == null) {                                                                                                                                                                                                          throw                                                                                                                                    new                                                                              IllegalArgumentException(                                                        "uri cannot be null"                                                        );                                                                                                          }                                                                                                                                                                InputSource input =                                                                            new                                                                                                                                    InputSource                                                        (uri);                                                                                                                                                                                                          this                                                        .                                                        parse                                                        (input, hb);                                                                                        }                                                                                

ryanalaingleuted.blogspot.com

Source: https://www.tabnine.com/code/java/methods/javax.xml.parsers.SAXParser/parse

0 Response to "Java Sax Parser Example Inputstream"

Postar um comentário

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel