Search in sources :

Example 41 with Document

use of org.w3c.dom.Document in project camel by apache.

the class XmlConverterTest method testToInputStreamFromDocument.

public void testToInputStreamFromDocument() throws Exception {
    XmlConverter conv = new XmlConverter();
    Document doc = context.getTypeConverter().convertTo(Document.class, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><foo>bar</foo>");
    InputStream is = conv.toInputStream(doc, null);
    assertNotNull(is);
    assertEquals("<foo>bar</foo>", context.getTypeConverter().convertTo(String.class, is));
}
Also used : InputStream(java.io.InputStream) Document(org.w3c.dom.Document)

Example 42 with Document

use of org.w3c.dom.Document in project camel by apache.

the class XmlConverterTest method testToStringWithDocumentSourceOutputProperties.

public void testToStringWithDocumentSourceOutputProperties() throws Exception {
    XmlConverter conv = new XmlConverter();
    Document document = conv.createDocument();
    Element foo = document.createElement("foo");
    foo.setTextContent("bar");
    document.appendChild(foo);
    Properties properties = new Properties();
    properties.put(OutputKeys.ENCODING, "ISO-8859-1");
    String out = conv.toStringFromDocument(document, properties);
    assertEquals("<?xml version=\"1.0\" encoding=\"ISO-8859-1\" standalone=\"no\"?><foo>bar</foo>", out);
}
Also used : Element(org.w3c.dom.Element) Document(org.w3c.dom.Document) Properties(java.util.Properties)

Example 43 with Document

use of org.w3c.dom.Document in project camel by apache.

the class XmlConverterTest method testToDomElementFromElementNode.

public void testToDomElementFromElementNode() throws Exception {
    XmlConverter conv = new XmlConverter();
    Document doc = context.getTypeConverter().convertTo(Document.class, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><foo>bar</foo>");
    Element out = conv.toDOMElement(doc.getDocumentElement());
    assertNotNull(out);
    assertEquals("<foo>bar</foo>", context.getTypeConverter().convertTo(String.class, out));
}
Also used : Element(org.w3c.dom.Element) Document(org.w3c.dom.Document)

Example 44 with Document

use of org.w3c.dom.Document in project camel by apache.

the class DomConverterTest method testDomConverterToInteger.

public void testDomConverterToInteger() throws Exception {
    Document document = context.getTypeConverter().convertTo(Document.class, "<?xml version=\"1.0\" encoding=\"UTF-8\"?><hello>47</hello>");
    Integer number = DomConverter.toInteger(document.getChildNodes());
    assertEquals(47, number.intValue());
}
Also used : Document(org.w3c.dom.Document)

Example 45 with Document

use of org.w3c.dom.Document in project camel by apache.

the class DomConverterTest method testDomConverterToList.

public void testDomConverterToList() throws Exception {
    Document document = context.getTypeConverter().convertTo(Document.class, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "<foo><hello>Hello World</hello><bye>Bye Camel</bye></foo>");
    List<?> list = DomConverter.toList(document.getElementsByTagName("foo"));
    assertEquals(1, list.size());
    NodeList nl = assertIsInstanceOf(NodeList.class, list.get(0));
    List<?> sub = DomConverter.toList(nl);
    assertEquals(2, sub.size());
    assertEquals("<hello>Hello World</hello>", new DomConverter().toString((NodeList) sub.get(0), null));
    assertEquals("<bye>Bye Camel</bye>", new DomConverter().toString((NodeList) sub.get(1), null));
}
Also used : NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document)

Aggregations

Document (org.w3c.dom.Document)2446 Element (org.w3c.dom.Element)990 DocumentBuilder (javax.xml.parsers.DocumentBuilder)719 NodeList (org.w3c.dom.NodeList)648 Node (org.w3c.dom.Node)545 DocumentBuilderFactory (javax.xml.parsers.DocumentBuilderFactory)528 IOException (java.io.IOException)425 SAXException (org.xml.sax.SAXException)301 ParserConfigurationException (javax.xml.parsers.ParserConfigurationException)299 InputSource (org.xml.sax.InputSource)250 Test (org.junit.Test)233 File (java.io.File)190 StringReader (java.io.StringReader)182 ArrayList (java.util.ArrayList)174 InputStream (java.io.InputStream)167 DOMSource (javax.xml.transform.dom.DOMSource)161 ByteArrayInputStream (java.io.ByteArrayInputStream)154 Attr (org.w3c.dom.Attr)134 DOMException (org.w3c.dom.DOMException)129 XPath (javax.xml.xpath.XPath)107