Search in sources :

Example 11 with Document

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

the class CxfJavaOnlyPayloadModeTest method testCxfJavaOnly.

@Test
public void testCxfJavaOnly() throws Exception {
    String s = "<GetPerson xmlns=\"http://camel.apache.org/wsdl-first/types\"><personId>123</personId></GetPerson>";
    Document xml = context.getTypeConverter().convertTo(Document.class, s);
    Object output = template.requestBody(url, xml);
    assertNotNull(output);
    // using CxfPayload in payload mode
    CxfPayload<?> payload = (CxfPayload<?>) output;
    // convert the payload body to string
    String reply = context.getTypeConverter().convertTo(String.class, payload.getBody().get(0));
    assertNotNull(reply);
    assertTrue(reply.contains("<personId>123</personId"));
    assertTrue(reply.contains("<ssn>456</ssn"));
    assertTrue(reply.contains("<name>Donald Duck</name"));
}
Also used : Document(org.w3c.dom.Document) Test(org.junit.Test)

Example 12 with Document

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

the class XPathTransformTest method testXPathTransform.

public void testXPathTransform() throws Exception {
    Document doc = context.getTypeConverter().convertTo(Document.class, "<root><firstname>Apache</firstname><lastname>Camel</lastname></root>");
    NodeList list = XPathBuilder.xpath("/root/firstname", NodeList.class).evaluate(context, doc, NodeList.class);
    assertNotNull(list);
    list.item(0).setTextContent("Servicemix");
    String out = context.getTypeConverter().convertTo(String.class, doc);
    assertEquals("<root><firstname>Servicemix</firstname><lastname>Camel</lastname></root>", out);
}
Also used : NodeList(org.w3c.dom.NodeList) Document(org.w3c.dom.Document)

Example 13 with Document

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

the class XPathTransformTest method testXPathNamespaceLoggingEnabledJavaDSL.

public void testXPathNamespaceLoggingEnabledJavaDSL() throws Exception {
    Logger l = createNiceMock(Logger.class);
    expect(l.isInfoEnabled()).andReturn(true).anyTimes();
    l.info(contains("Namespaces discovered in message"), anyObject(Object.class));
    expectLastCall().times(1);
    replay(l);
    String body = "<aRoot xmlns:nsa=\"http://namespacec.net\"><nsa:a xmlns:nsa=\"http://namespacea.net\">Hello|there|Camel</nsa:a>" + "<nsb:a xmlns:nsb=\"http://namespaceb.net\">Hello|there|Camel</nsb:a><nsb:a xmlns:nsb=\"http://namespaceb.net\">Hello|there|Camel</nsb:a>" + "<a xmlns=\"http://defaultNamespace.net\">Hello|there|Camel</a><a>Hello|there|Camel</a></aRoot>";
    Document doc = context.getTypeConverter().convertTo(Document.class, body);
    Field logField = XPathBuilder.class.getDeclaredField("LOG");
    logField.setAccessible(true);
    Field modifiers = Field.class.getDeclaredField("modifiers");
    modifiers.setAccessible(true);
    modifiers.setInt(logField, logField.getModifiers() & ~Modifier.FINAL);
    logField.set(null, l);
    NodeList list = XPathBuilder.xpath("//*", NodeList.class).logNamespaces().evaluate(context, doc, NodeList.class);
    assertNotNull(list);
    verify(l);
}
Also used : Field(java.lang.reflect.Field) NodeList(org.w3c.dom.NodeList) EasyMock.anyObject(org.easymock.EasyMock.anyObject) Logger(org.slf4j.Logger) Document(org.w3c.dom.Document)

Example 14 with Document

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

the class XPathTest method testXPathNodeListSimpleTestText.

public void testXPathNodeListSimpleTestText() throws Exception {
    String xml = "<foo><person>Claus</person></foo>";
    Document doc = context.getTypeConverter().convertTo(Document.class, xml);
    Object result = xpath("/foo/person/text()").nodeSetResult().evaluate(createExchange(doc));
    assertNotNull(result);
    String s = context.getTypeConverter().convertTo(String.class, result);
    assertEquals("Claus", s);
}
Also used : Document(org.w3c.dom.Document)

Example 15 with Document

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

the class XPathTest method testXPathNodeListTest.

public void testXPathNodeListTest() throws Exception {
    String xml = "<foo><person id=\"1\">Claus<country>SE</country></person>" + "<person id=\"2\">Jonathan<country>CA</country></person></foo>";
    Document doc = context.getTypeConverter().convertTo(Document.class, xml);
    Object result = xpath("/foo/person").nodeSetResult().evaluate(createExchange(doc));
    assertNotNull(result);
    String s = context.getTypeConverter().convertTo(String.class, result);
    assertEquals(ObjectHelper.between(xml, "<foo>", "</foo>"), s);
}
Also used : 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