Java 8 DOMSource - 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 DOMSource class.

This code example shows how to use the following methods:
 
	@Test
	public void unmarshalDomSource() throws Exception {
		DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
		Document document = builder.parse(new InputSource(new StringReader(INPUT_STRING)));
		DOMSource source = new DOMSource(document);
		Object flight = unmarshaller.unmarshal(source);
		testFlight(flight); 
	} 
 
	@Test
	public void unmarshalStaxSourceXmlStreamReader() throws Exception {
		XMLInputFactory inputFactory = XMLInputFactory.newInstance();
		XMLStreamReader streamReader = inputFactory.createXMLStreamReader(new StringReader(INPUT_STRING));
		Source source = StaxUtils.createStaxSource(streamReader);
		Object flights = unmarshaller.unmarshal(source);
		testFlight(flights); 
	} 
 
	@Test
10
CodeRank
This code example shows how to use the following methods:
		flightsElement.appendChild(flightElement);
		Element numberElement = document.createElementNS("http://samples.springframework.org/flight", "tns:number");
		flightElement.appendChild(numberElement);
		Text text = document.createTextNode("42");
		numberElement.appendChild(text);
		DOMSource source = new DOMSource(document);
		Object flights = unmarshaller.unmarshal(source);
		testFlights(flights); 
	} 
 
	@Test
	public void unmarshalStreamSourceReader() throws Exception {
		StreamSource source = new StreamSource(new StringReader(INPUT_STRING));
		Object flights = unmarshaller.unmarshal(source);
		testFlights(flights); 
	} 
 
	@Test
	public void unmarshalStreamSourceInputStream() throws Exception {
		StreamSource source = new StreamSource(new ByteArrayInputStream(INPUT_STRING.getBytes("UTF-8")));
10
CodeRank
This code example shows how to use the following methods:
	 * 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 
	 * regardless of order. 
	 * <p>Use of this method assumes the 
	 * <a href="http://xmlunit.sourceforge.net/">XMLUnit<a/> library is available. 
	 * @param expected the expected XML content 
	 * @param actual the actual XML content 
	 * @see org.springframework.test.web.servlet.result.MockMvcResultMatchers#xpath(String, Object...) 
	 * @see org.springframework.test.web.servlet.result.MockMvcResultMatchers#xpath(String, Map, Object...) 
	 */ 
	public void assertXmlEqual(String expected, String actual) throws Exception {
10
CodeRank
This code example shows how to use the following methods:
            transformer.setOutputProperty(OutputKeys.METHOD, "xml");
            transformer.setOutputProperty(OutputKeys.INDENT, "yes");
            transformer.setOutputProperty(OutputKeys.ENCODING, "UTF-8");
 
            //need to nest outputStreamWriter to get around JDK 5 bug.  See http://bugs.sun.com/bugdatabase/view_bug.do?bug_id=6296446 
            transformer.transform(new DOMSource(doc), new StreamResult(new OutputStreamWriter(outputStream, "utf-8")));
        } catch (TransformerException e) {
            throw new IOException(e.getMessage());
        } 
    } 
} 
10
CodeRank
This code example shows how to use the following methods:
    public static String serialize(Document document) {
        StringWriter writer = new StringWriter();
        try { 
            TransformerFactory factory = TransformerFactory.newInstance();
            Transformer transformer = factory.newTransformer();
            DOMSource domSource = new DOMSource(document);
            StreamResult streamResult = new StreamResult(writer);
            transformer.transform(domSource, streamResult); 
        } catch (TransformerException e) {
            throw new RuntimeException(
                    "Error when serializing XML document.", e);
        } 
        return writer.toString();
    } 
 
    /** 
     * Parse an XML file to DOM 
     * @return null if an error occurs during parsing. 
     *  
     */ 
8
CodeRank
This code example shows how to use the following methods:
This code example shows how to use the following methods:
     
 
    // 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>"));
  } 
} 
8
CodeRank
This code example shows how to use the following methods:
            //Setup indenting to "pretty print" 
            serializer.setOutputProperty(OutputKeys.INDENT, "yes");
            serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
 
            Element document = panel.getRootBox().getElement();
            DOMSource source = new DOMSource(document);
            StreamResult output = new StreamResult(System.out);
            serializer.transform(
                    source,
                    output
            ); 
        } catch (TransformerException ex) {
            // this is fatal, just dump the stack and throw a runtime exception 
            ex.printStackTrace();
 
            throw new RuntimeException(ex);
        } 
    } 
} 
10
CodeRank
This code example shows how to use the following methods:
     
 
    // 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>"));
  } 
} 
10
CodeRank
This code example shows how to use the following methods:

Related Java 8 DOMSource Questions & Answers:

See Code Examples for Java 8 DOMSource Methods: