Search in sources :

Example 1 with XMLCell

use of org.knime.core.data.xml.XMLCell in project knime-core by knime.

the class JavaToDataCellConversionTest method testToXMLCell.

/**
 * Test String -> XMLCell, Document -> XMLCell and InputStream -> XMLCell conversions.
 *
 * @throws Exception When something went wrong
 */
@Test
public void testToXMLCell() throws Exception {
    final String xmlString = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n<KNIME>\n</KNIME>";
    /* from String */
    {
        final XMLCell xmlCell = testSimpleConversion(String.class, XMLCell.TYPE, XMLCell.class, new String("<KNIME />"));
        assertEquals(xmlString, xmlCell.getStringValue().replace('\'', '"'));
    }
    /* from Document */
    {
        final DocumentBuilderFactory documentBuilderFactory = DocumentBuilderFactory.newInstance();
        assertNotNull(documentBuilderFactory);
        final DocumentBuilder builder = documentBuilderFactory.newDocumentBuilder();
        final XMLCell xmlCell = testSimpleConversion(Document.class, XMLCell.TYPE, XMLCell.class, builder.parse(new InputSource(new StringReader(xmlString))));
        assertEquals(xmlString, xmlCell.getStringValue().replace('\'', '"'));
    }
    /* from InputStream */
    try (final InputStream stream = getClass().getResourceAsStream("test.xml")) {
        assertTrue(stream.available() > 0);
        final XMLCell xmlCell = testSimpleConversion(InputStream.class, XMLCell.TYPE, XMLCell.class, stream);
        assertEquals(xmlString, xmlCell.getStringValue().replace('\'', '"'));
    }
}
Also used : XMLCell(org.knime.core.data.xml.XMLCell) InputSource(org.xml.sax.InputSource) DocumentBuilderFactory(javax.xml.parsers.DocumentBuilderFactory) DocumentBuilder(javax.xml.parsers.DocumentBuilder) ByteArrayInputStream(java.io.ByteArrayInputStream) FileInputStream(java.io.FileInputStream) InputStream(java.io.InputStream) StringReader(java.io.StringReader) Document(org.w3c.dom.Document) Test(org.junit.Test)

Aggregations

ByteArrayInputStream (java.io.ByteArrayInputStream)1 FileInputStream (java.io.FileInputStream)1 InputStream (java.io.InputStream)1 StringReader (java.io.StringReader)1 DocumentBuilder (javax.xml.parsers.DocumentBuilder)1 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)1 Test (org.junit.Test)1 XMLCell (org.knime.core.data.xml.XMLCell)1 Document (org.w3c.dom.Document)1 InputSource (org.xml.sax.InputSource)1