javax.xml.parsers.SAXParser.parse()方法的使用及代码示例

x33g5p2x  于2022-01-29 转载在 其他  
字(11.8k)|赞(0)|评价(0)|浏览(246)

本文整理了Java中javax.xml.parsers.SAXParser.parse()方法的一些代码示例,展示了SAXParser.parse()的具体用法。这些代码示例主要来源于Github/Stackoverflow/Maven等平台,是从一些精选项目中提取出来的代码,具有较强的参考意义,能在一定程度帮忙到你。SAXParser.parse()方法的具体详情如下:
包路径:javax.xml.parsers.SAXParser
类名称:SAXParser
方法名:parse

SAXParser.parse介绍

[英]Parse the content of the file specified as XML using the specified org.xml.sax.HandlerBase. Use of the DefaultHandler version of this method is recommended as the HandlerBase class has been deprecated in SAX 2.0
[中]使用指定的组织解析指定为XML的文件的内容。xml。萨克斯。HandlerBase。建议使用此方法的DefaultHandler版本,因为SAX 2.0中不推荐使用HandlerBase类

代码示例

代码示例来源:origin: stackoverflow.com

SAXParserFactory factory = SAXParserFactory.newInstance();
 factory.setValidating(true);
 try {
   SAXParser saxParser = factory.newSAXParser();
   File file = new File("test.xml");
   saxParser.parse(file, new ElementHandler());    // specify handler
 }
 catch(ParserConfigurationException e1) {
 }
 catch(SAXException e1) {
 }
 catch(IOException e) {
 }

代码示例来源:origin: org.hamcrest/hamcrest-all

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);
        }
      }
    }
  });
}

代码示例来源:origin: robovm/robovm

/**
 * Parse the content of the given {@link java.io.InputStream}
 * instance as XML using the specified
 * {@link org.xml.sax.helpers.DefaultHandler}.
 *
 * @param is InputStream containing the content to be parsed.
 * @param dh The SAX DefaultHandler to use.
 *
 * @throws IllegalArgumentException If the given InputStream is null.
 * @throws IOException If any IO errors occur.
 * @throws SAXException If any SAX errors occur during processing.
 *
 * @see org.xml.sax.DocumentHandler
 */
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);
}

代码示例来源:origin: stackoverflow.com

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);
  //TODO: get the data from your handler
}
catch (final Exception e)
{
  Log.e("ParseError", "Error parsing xml", e);
}

代码示例来源:origin: jenkinsci/jenkins

InputSource input = new InputSource(file.toURI().toASCIIString());
input.setByteStream(in);
JAXP.newSAXParser().parse(input,new DefaultHandler() {
  private Locator loc;
  @Override

代码示例来源:origin: hawtio/hawtio

public Set<String> parseContents(InputSource contents) throws IOException, ParserConfigurationException, SAXException {
  namespaces.clear();
  // Parse the file into we have what we need (or an error occurs).
  if (factory == null) {
    factory = SAXParserFactory.newInstance();
  }
  if (factory != null) {
    SAXParser parser = createParser(factory);
    // to support external entities specified as relative URIs (see bug 63298)
    contents.setSystemId("/"); //$NON-NLS-1$
    parser.parse(contents, this);
  }
  return namespaces;
}

代码示例来源:origin: camunda/camunda-bpm-platform

public Parse execute() {
 try {
  InputStream inputStream = streamSource.getInputStream();
  parser.getSaxParserFactory().setFeature(XXE_PROCESSING, enableXxeProcessing);
  if (schemaResource == null) { // must be done before parser is created
   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;
}

代码示例来源:origin: languagetool-org/languagetool

private void validateInternal(String xml, String dtdPath, String docType) throws SAXException, IOException, ParserConfigurationException {
 SAXParserFactory factory = SAXParserFactory.newInstance();
 factory.setValidating(true);
 SAXParser saxParser = factory.newSAXParser();
 //used for removing existing DOCTYPE from grammar.xml files
 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());
}

代码示例来源:origin: apache/zookeeper

/** Creates a new instance of BinaryInputArchive */
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;
}

代码示例来源:origin: robovm/robovm

/**
 * Parse the content described by the giving Uniform Resource
 * Identifier (URI) as XML using the specified
 * {@link org.xml.sax.helpers.DefaultHandler}.
 *
 * @param uri The location of the content to be parsed.
 * @param dh The SAX DefaultHandler to use.
 *
 * @throws IllegalArgumentException If the uri is null.
 * @throws IOException If any IO errors occur.
 * @throws SAXException If any SAX errors occur during processing.
 *
 * @see org.xml.sax.DocumentHandler
 */
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);
}

代码示例来源:origin: bobbylight/RSyntaxTextArea

/**
 * {@inheritDoc}
 */
@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) {
    // A fatal parse error - ignore; a ParserNotice was already created.
  } catch (Exception e) {
    //e.printStackTrace(); // Will print if DTD specified and can't be found
    result.addNotice(new DefaultParserNotice(this,
        "Error parsing XML: " + e.getMessage(), 0, -1, -1));
  }
  return result;
}

代码示例来源:origin: pentaho/pentaho-kettle

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 );
}

代码示例来源:origin: camunda/camunda-bpm-platform

public Parse execute() {
 try {
  InputStream inputStream = streamSource.getInputStream();
  parser.getSaxParserFactory().setFeature(XXE_PROCESSING, enableXxeProcessing);
  if (schemaResource == null) { // must be done before parser is created
   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;
}

代码示例来源:origin: MovingBlocks/Terasology

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);
    }
  }
}

代码示例来源:origin: jenkinsci/jenkins

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");

代码示例来源:origin: robovm/robovm

/**
 * <p>Parse the content of the given {@link java.io.InputStream}
 * instance as XML using the specified {@link org.xml.sax.HandlerBase}.
 * <i> Use of the DefaultHandler version of this method is recommended as
 * the HandlerBase class has been deprecated in SAX 2.0</i>.</p>
 *
 * @param is InputStream containing the content to be parsed.
 * @param hb The SAX HandlerBase to use.
 *
 * @throws IllegalArgumentException If the given InputStream is null.
 * @throws SAXException If parse produces a SAX error.
 * @throws IOException If an IO error occurs interacting with the
 *   <code>InputStream</code>.
 *
 * @see org.xml.sax.DocumentHandler
 */
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);
}

代码示例来源:origin: pentaho/pentaho-kettle

/**
 * Checks an xml string is well formed.
 *
 * @param is
 *          inputstream
 * @return true if the xml is well formed.
 */
public static boolean isXMLWellFormed( InputStream is ) throws KettleException {
 boolean retval = false;
 try {
  SAXParserFactory factory = XMLParserFactoryProducer.createSecureSAXParserFactory();
  XMLTreeHandler handler = new XMLTreeHandler();
  // Parse the input.
  SAXParser saxParser = factory.newSAXParser();
  saxParser.parse( is, handler );
  retval = true;
 } catch ( Exception e ) {
  throw new KettleException( e );
 }
 return retval;
}

代码示例来源:origin: geoserver/geoserver

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);

代码示例来源:origin: JpressProjects/jpress

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);
  }
}

代码示例来源:origin: robovm/robovm

/**
 * Parse the content described by the giving Uniform Resource
 * Identifier (URI) as XML using the specified
 * {@link org.xml.sax.HandlerBase}.
 * <i> Use of the DefaultHandler version of this method is recommended as
 * the <code>HandlerBase</code> class has been deprecated in SAX 2.0</i>
 *
 * @param uri The location of the content to be parsed.
 * @param hb The SAX HandlerBase to use.
 *
 * @throws IllegalArgumentException If the uri is null.
 * @throws IOException If any IO errors occur.
 * @throws SAXException If any SAX errors occur during processing.
 *
 * @see org.xml.sax.DocumentHandler
 */
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);
}

相关文章