/** * Validate the given stream and return a valid DOM document for parsing. */ protected Document buildDocument(ErrorHandler handler, InputStream stream) throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); dbf.setNamespaceAware(true); DocumentBuilder parser = dbf.newDocumentBuilder(); parser.setErrorHandler(handler); return parser.parse(stream); }
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); Document doc = builder.parse(<uri_as_string>); XPathFactory xPathfactory = XPathFactory.newInstance(); XPath xpath = xPathfactory.newXPath(); XPathExpression expr = xpath.compile(<xpath_expression>);
public Document parse(final DocumentBuilder parser) throws SAXException, IOException { return parser.parse(inputSource); } public String toString() {
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(true); factory.setIgnoringElementContentWhitespace(true); try { DocumentBuilder builder = factory.newDocumentBuilder(); File file = new File("test.xml"); Document doc = builder.parse(file); // Do something with the document here. } catch (ParserConfigurationException e) { } catch (SAXException e) { } catch (IOException e) { }
public static Document loadXMLFromString(String xml) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = factory.newDocumentBuilder(); InputSource is = new InputSource(new StringReader(xml)); return builder.parse(is); }
@Before @SuppressWarnings("deprecation") // on JDK 9 public void setUp() throws Exception { DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance(); documentBuilderFactory.setNamespaceAware(true); documentBuilder = documentBuilderFactory.newDocumentBuilder(); result = documentBuilder.newDocument(); xmlReader = org.xml.sax.helpers.XMLReaderFactory.createXMLReader(); }
public static Document createDocument() { try { DocumentBuilderFactory dbFactory = DocumentBuilderFactory.newInstance(); DocumentBuilder docBuilder = dbFactory.newDocumentBuilder(); Document doc = docBuilder.newDocument(); return doc; } catch (Exception e) { throw new RuntimeException(e); } }
private Document loadXML(String xml) { Document doc; try (InputStream xmlStream = ManifestAttributes.class.getResourceAsStream(xml)) { if (xmlStream == null) { throw new JadxRuntimeException(xml + " not found in classpath"); } DocumentBuilder dBuilder = XmlSecurity.getSecureDbf().newDocumentBuilder(); doc = dBuilder.parse(xmlStream); } catch (Exception e) { throw new JadxRuntimeException("Xml load error, file: " + xml, e); } return doc; }
throws ParserConfigurationException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(namespaceAware); factory.setValidating(true); if (validationMode == XmlValidationModeDetector.VALIDATION_XSD) { factory.setNamespaceAware(true); try { factory.setAttribute(SCHEMA_LANGUAGE_ATTRIBUTE, XSD_SCHEMA_LANGUAGE); ParserConfigurationException pcex = new ParserConfigurationException( "Unable to validate using XSD: Your JAXP provider [" + factory + "] does not support XML Schema. Are you running on Java 1.4 with Apache Crimson? " + "Upgrade to Apache Xerces (or Java 1.5) for full XSD support."); pcex.initCause(ex); throw pcex;
/** * Create a {@code DocumentBuilder} that this marshaller will use for creating * DOM documents when passed an empty {@code DOMSource}. * <p>The resulting {@code DocumentBuilderFactory} is cached, so this method * will only be called once. * @return the DocumentBuilderFactory * @throws ParserConfigurationException if thrown by JAXP methods */ protected DocumentBuilderFactory createDocumentBuilderFactory() throws ParserConfigurationException { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setValidating(false); factory.setNamespaceAware(true); factory.setFeature("http://apache.org/xml/features/disallow-doctype-decl", !isSupportDtd()); factory.setFeature("http://xml.org/sax/features/external-general-entities", isProcessExternalEntities()); return factory; }
/** * Create a JAXP DocumentBuilder that this bean definition reader * will use for parsing XML documents. Can be overridden in subclasses, * adding further initialization of the builder. * @param factory the JAXP DocumentBuilderFactory that the DocumentBuilder * should be created with * @param entityResolver the SAX EntityResolver to use * @param errorHandler the SAX ErrorHandler to use * @return the JAXP DocumentBuilder * @throws ParserConfigurationException if thrown by JAXP methods */ protected DocumentBuilder createDocumentBuilder(DocumentBuilderFactory factory, @Nullable EntityResolver entityResolver, @Nullable ErrorHandler errorHandler) throws ParserConfigurationException { DocumentBuilder docBuilder = factory.newDocumentBuilder(); if (entityResolver != null) { docBuilder.setEntityResolver(entityResolver); } if (errorHandler != null) { docBuilder.setErrorHandler(errorHandler); } return docBuilder; }
public static DocumentBuilderFactory getSecureDbf() throws ParserConfigurationException { synchronized (XmlSecurity.class) { if (secureDbf == null) { secureDbf = DocumentBuilderFactory.newInstance(); secureDbf.setFeature("http://apache.org/xml/features/disallow-doctype-decl", true); secureDbf.setFeature("http://apache.org/xml/features/nonvalidating/load-external-dtd", false); secureDbf.setFeature("http://xml.org/sax/features/external-general-entities", false); secureDbf.setFeature("http://xml.org/sax/features/external-parameter-entities", false); secureDbf.setFeature("http://apache.org/xml/features/dom/create-entity-ref-nodes", false); secureDbf.setXIncludeAware(false); secureDbf.setExpandEntityReferences(false); } } return secureDbf; } }
/** * Create a {@code DocumentBuilder} that this marshaller will use for creating * DOM documents when passed an empty {@code DOMSource}. * <p>Can be overridden in subclasses, adding further initialization of the builder. * @param factory the {@code DocumentBuilderFactory} that the DocumentBuilder should be created with * @return the {@code DocumentBuilder} * @throws ParserConfigurationException if thrown by JAXP methods */ protected DocumentBuilder createDocumentBuilder(DocumentBuilderFactory factory) throws ParserConfigurationException { DocumentBuilder documentBuilder = factory.newDocumentBuilder(); if (!isProcessExternalEntities()) { documentBuilder.setEntityResolver(NO_OP_ENTITY_RESOLVER); } return documentBuilder; }
/** * Overridable factory method used to create {@link DocumentBuilder} for parsing * XML as DOM. * * @since 2.7.6 */ protected DocumentBuilder documentBuilder() throws ParserConfigurationException { return DEFAULT_PARSER_FACTORY.newDocumentBuilder(); }
class RetrieveFeedTask extends AsyncTask<String, Void, RSSFeed> { private Exception exception; protected RSSFeed doInBackground(String... urls) { try { URL url = new URL(urls[0]); SAXParserFactory factory = SAXParserFactory.newInstance(); SAXParser parser = factory.newSAXParser(); XMLReader xmlreader = parser.getXMLReader(); RssHandler theRSSHandler = new RssHandler(); xmlreader.setContentHandler(theRSSHandler); InputSource is = new InputSource(url.openStream()); xmlreader.parse(is); return theRSSHandler.getFeed(); } catch (Exception e) { this.exception = e; return null; } } protected void onPostExecute(RSSFeed feed) { // TODO: check this.exception // TODO: do something with the feed } }
public void parse(InputStream is, DefaultHandler dh) throws SAXException, IOException { synchronized (m_saxParser) { m_saxParser.parse(is, dh); } }
/** * Tests if the current <code>SAXParserFactory</code> supports DTD validation. */ private static boolean supportsValidation(SAXParserFactory spf) { try { spf.getFeature("http://xml.org/sax/features/validation"); return true; } catch(Exception ex) { return false; } }
private Document parseXmlString(String xml) throws Exception { DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); factory.setNamespaceAware(true); DocumentBuilder documentBuilder = factory.newDocumentBuilder(); InputSource inputSource = new InputSource(new StringReader(xml)); return documentBuilder.parse(inputSource); }
private org.w3c.dom.Document getDomDocument() throws ParserConfigurationException, SAXException, IOException { DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); DocumentBuilder builder = dbf.newDocumentBuilder(); org.w3c.dom.Document document = builder.parse(getProductDataResource().getInputStream()); return document; }
/** * Overridable factory method used to create {@link DocumentBuilder} for parsing * XML as DOM. * * @since 2.7.6 */ protected DocumentBuilder documentBuilder() throws ParserConfigurationException { return DEFAULT_PARSER_FACTORY.newDocumentBuilder(); }