Example usage for org.w3c.dom Text getNextSibling

List of usage examples for org.w3c.dom Text getNextSibling

Introduction

In this page you can find the example usage for org.w3c.dom Text getNextSibling.

Prototype

public Node getNextSibling();

Source Link

Document

The node immediately following this node.

Usage

From source file:Main.java

/**
 * Return the content of the given element.
 * <p/>/*  w  w  w .  jav a2s  .  co m*/
 * We will descend to an arbitrary depth looking for the first text node.
 * <p/>
 * Note that the parser may break what was originally a single string of
 * pcdata into multiple adjacent text nodes. Xerces appears to do this when
 * it encounters a '$' in the text, not sure if there is specified behavior,
 * or if its parser specific.
 * <p/>
 * Here, we will congeal adjacent text nodes.
 * <p/>
 * We will NOT ignore text nodes that have only whitespace.
 */
public static String getContent(Element e) {

    String content = null;

    if (e != null) {

        // find the first inner text node,
        Text t = findText(e, false);
        if (t != null) {
            // we have at least some text
            StringBuilder b = new StringBuilder();
            while (t != null) {
                b.append(t.getData());
                Node n = t.getNextSibling();

                t = null;
                if (n != null && ((n.getNodeType() == Node.TEXT_NODE)
                        || (n.getNodeType() == Node.CDATA_SECTION_NODE))) {
                    t = (Text) n;
                }
            }
            content = b.toString();
        }
    }

    return content;
}

From source file:org.apache.camel.component.cxf.CxfConsumerPayloadXPathClientServerTest.java

@Override
protected RouteBuilder createRouteBuilder() {
    return new RouteBuilder() {
        @Override/*w  w  w  .  java  2 s  .c  o m*/
        public void configure() {
            from(simpleEndpointURI + "&dataFormat=PAYLOAD").to("log:info").process(new Processor() {
                @SuppressWarnings("unchecked")
                @Override
                public void process(final Exchange exchange) throws Exception {
                    Object request = exchange.getIn().getBody();
                    assertIsInstanceOf(CxfPayload.class, request);

                    //attempt 1) applying XPath to exchange.getIn().getBody()
                    receivedMessageCxfPayloadApplyingXPath = XPathBuilder.xpath("//*[name()='arg0']/text()")
                            .evaluate(context, request, String.class);

                    //attempt 2) in stead of XPATH, browse the DOM-tree
                    CxfPayload<SoapHeader> payload = (CxfPayload<SoapHeader>) request;
                    Element el = payload.getBody().get(0);
                    Element el2 = (Element) el.getFirstChild();
                    Text textnode = (Text) el2.getFirstChild();
                    receivedMessageByDom = textnode.getNodeValue();

                    textnode = (Text) textnode.getNextSibling();
                    while (textnode != null) {
                        //the textnode appears to have siblings!
                        receivedMessageByDom = receivedMessageByDom + textnode.getNodeValue();
                        textnode = (Text) textnode.getNextSibling();
                    }

                    //attempt 3) apply XPATH after converting CxfPayload to String
                    request = exchange.getIn().getBody(String.class);
                    assertIsInstanceOf(String.class, request);
                    receivedMessageStringApplyingXPath = XPathBuilder.xpath("//*[name()='arg0']/text()")
                            .evaluate(context, request, String.class);

                    //build some dummy response 
                    XmlConverter converter = new XmlConverter();
                    Document outDocument = converter.toDOMDocument(ECHO_RESPONSE);
                    List<Source> outElements = new ArrayList<Source>();
                    outElements.add(new DOMSource(outDocument.getDocumentElement()));
                    // set the payload header with null
                    CxfPayload<SoapHeader> responsePayload = new CxfPayload<SoapHeader>(null, outElements,
                            null);
                    exchange.getOut().setBody(responsePayload);
                }
            });
        }
    };
}