Java 8 DocumentBuilder - top ranked examples from Open Source projects

These code examples were ranked by Codota’s semantic indexing as the best open source examples for Java 8 DocumentBuilder class.

This code example shows how to use the following methods:parse, setEntityResolver, setErrorHandler
		DocumentBuilderFactory factory = createDocumentBuilderFactory(validationMode, namespaceAware);
		if (logger.isDebugEnabled()) {
			logger.debug("Using JAXP provider [" + factory.getClass().getName() + "]");
		} 
		DocumentBuilder builder = createDocumentBuilder(factory, entityResolver, errorHandler);
		return builder.parse(inputSource);
	} 
 
	/** 
	 * Create the {@link DocumentBuilderFactory} instance. 
	 * @param validationMode the type of validation: {@link XmlValidationModeDetector#VALIDATION_DTD DTD} 
	 * or {@link XmlValidationModeDetector#VALIDATION_XSD XSD}) 
	 * @param namespaceAware whether the returned factory is to provide support for XML namespaces 
	 * @return the JAXP DocumentBuilderFactory 
	 * @throws ParserConfigurationException if we failed to build a proper DocumentBuilderFactory 
	 */ 
	protected DocumentBuilderFactory createDocumentBuilderFactory(int validationMode, boolean namespaceAware)
			throws ParserConfigurationException {
 
		DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();

8
CodeRank
This code example shows how to use the following methods:newDocument
    DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
        .newInstance();
    DocumentBuilder builder = docBuilderFactory.newDocumentBuilder();
     
     
    Document document = builder.newDocument();
     
 
    // test QueueConfigurationParser.getQueueElement  
    Element e = QueueConfigurationParser.getQueueElement(document, info);
    // transform result to string for check 
    DOMSource domSource = new DOMSource(e);
    StringWriter writer = new StringWriter();
    StreamResult result = new StreamResult(writer);
    TransformerFactory tf = TransformerFactory.newInstance();
    Transformer transformer = tf.newTransformer();
    transformer.transform(domSource, result);
    String str= writer.toString();
    assertTrue(str
        .endsWith("<queue><name>root</name><properties/><state>running</state><queue><name>child1</name><properties/><state>running</state></queue><queue><name>child2</name><properties/><state>running</state></queue></queue>"));
7
CodeRank
This code example shows how to use the following methods:setEntityResolver, setErrorHandler
        StreamSource ss = new StreamSource(is);
        Schema schema = schemaFactory.newSchema(ss);
        dbf.setSchema(schema);
 
        builder = dbf.newDocumentBuilder();
        builder.setErrorHandler(handler);
        builder.setEntityResolver(handler);
        validator = schema.newValidator();
    } 
 
    @Override 
    protected void tearDown() throws Exception { 
        builder = null;
        validator = null;
        super.tearDown(); 
    } 
 
    public void testParserBadXML() { 
        System.out.println("-- testParserBadXML");
        try { 
7
CodeRank
This code example shows how to use the following methods:parse
 
			// Convert the string to an input stream. 
			InputStream is = new ByteArrayInputStream(xml.getBytes("UTF-8"));
 
			documentBuilder = factory.newDocumentBuilder();
			document = documentBuilder.parse(is);
		} 
		catch (Exception e)
		{ 
			TapjoyLog.e(TAPJOY_UTIL, "buildDocument exception: " + e.toString());
		} 
 
		return document;
	} 
 
	/** 
	 * Helper class to help parse the server response. 
	 *  
	 * @param nodeList 
	 * @return 
7
CodeRank
This code example shows how to use the following methods:newDocument, parse
		XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(XML));
		StaxSource source = new StaxSource(streamReader);
		assertEquals("Invalid streamReader returned", streamReader, source.getXMLStreamReader());
		assertNull("EventReader returned", source.getXMLEventReader());
 
		Document expected = documentBuilder.parse(new InputSource(new StringReader(XML)));
		Document result = documentBuilder.newDocument();
		transformer.transform(source, new DOMResult(result));
		assertXMLEqual("Invalid result", expected, result);
	} 
 
	@Test
	public void eventReaderSourceToStreamResult() throws Exception {
		XMLEventReader eventReader = inputFactory.createXMLEventReader(new StringReader(XML));
		StaxSource source = new StaxSource(eventReader);
		assertEquals("Invalid eventReader returned", eventReader, source.getXMLEventReader());
		assertNull("StreamReader returned", source.getXMLStreamReader());
		StringWriter writer = new StringWriter();
		transformer.transform(source, new StreamResult(writer));
		assertXMLEqual("Invalid result", XML, writer.toString());
7
CodeRank
This code example shows how to use the following methods:parse
	 
	protected Document parseDocument(String contents) {
		try { 
			DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
			DocumentBuilder builder = factory.newDocumentBuilder();
			return builder.parse(new InputSource(new StringReader(contents)));
		} catch (Exception e) {
			throw new RuntimeException(e);
		}  
	} 
	 
	public String name() {
		return "xhtml"; 
	} 
	 
	public SeleniumTest process(String contents) {
		String name;
		NSMutableArray elements = new NSMutableArray();
		Document document = parseDocument(contents);
		 
7
CodeRank
This code example shows how to use the following methods:newDocument
	@Before 
	public void setUp() throws Exception {
		DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
		documentBuilderFactory.setNamespaceAware(true);
		documentBuilder = documentBuilderFactory.newDocumentBuilder();
		result = documentBuilder.newDocument();
		xmlReader = XMLReaderFactory.createXMLReader();
	} 
 
	@Test
	public void contentHandlerDocumentNamespacePrefixes() throws Exception {
		xmlReader.setFeature("http://xml.org/sax/features/namespace-prefixes", true);
		handler = new DomContentHandler(result);
		expected = documentBuilder.parse(new InputSource(new StringReader(XML_1)));
		xmlReader.setContentHandler(handler);
		xmlReader.parse(new InputSource(new StringReader(XML_1)));
		assertXMLEqual("Invalid result", expected, result);
	} 
 
	@Test
7
CodeRank
This code example shows how to use the following methods:parse, setEntityResolver, setErrorHandler
    	factoryToUse = ResourceLoader.nonValidatingDocumentBuilderFactory;
      } 
      inputStream = getResourceAsStream(requestingClass, resource);
      DocumentBuilder db = factoryToUse.newDocumentBuilder();
 
      db.setEntityResolver(new DTDResolver());
      db.setErrorHandler(new SAXErrorHandler("ResourceLoader.getResourceAsDocument(" + resource + ")"));
      document = db.parse(inputStream);
    } finally { 
      if (inputStream != null)
        inputStream.close();
    } 
    return document;
  } 
 
  /** 
   * Get the contents of a URL as an XML Document, first trying to read the Document with validation turned on, 
   * and falling back to reading it with validation turned off. 
   * @param requestingClass the java.lang.Class object of the class that is attempting to load the resource 
   * @param resource a String describing the full or partial URL of the resource whose contents to load 
7
CodeRank
This code example shows how to use the following methods:newDocument, parse
This code example shows how to use the following methods:parse
	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);
	} 
 
	/** 
	 * Parse the content as {@link DOMSource} and apply a {@link Matcher}. 
	 * @see <a href="http://code.google.com/p/xml-matchers/">xml-matchers</a> 
	 */ 
	public void assertSource(String content, Matcher<? super Source> matcher) throws Exception {
		Document document = parseXmlString(content);
		assertThat("Body content", new DOMSource(document), matcher);
	} 
 
	/** 
	 * Parse the expected and actual content strings as XML and assert that the 
	 * two are "similar" -- i.e. they contain the same elements and attributes 
7
CodeRank

Related Java 8 DocumentBuilder Questions & Answers:

Other Java 8 DocumentBuilder Examples

3:			try {
4:	
5:				DocumentBuilder db = dbf.newDocumentBuilder();
6:	
7:				InputSource is = new InputSource();

Read More

66:	
67:				DocumentBuilder db = dbf.newDocumentBuilder();
68:	
69:				InputSource is = new InputSource();
70:			        is.setCharacterStream(new StringReader(xml));

Read More

66:	
67:				DocumentBuilder db = dbf.newDocumentBuilder();
68:	
69:				InputSource is = new InputSource();
70:			        is.setCharacterStream(new StringReader(xml));

Read More

See Code Examples for Java 8 DocumentBuilder Methods: