@Validated @Test public void testAppendChild() throws Exception { MessageFactory fact = MessageFactory.newInstance(); SOAPMessage message = fact.createMessage(); SOAPHeader soapHeader = message.getSOAPHeader(); assertEquals(0, soapHeader.getChildNodes().getLength()); assertFalse(soapHeader.getChildElements().hasNext()); Document doc = soapHeader.getOwnerDocument(); String namespace = "http://example.com"; String localName = "GetLastTradePrice"; Element getLastTradePrice = doc.createElementNS(namespace, localName); Element symbol = doc.createElement("symbol"); symbol.setAttribute("foo", "bar"); getLastTradePrice.appendChild(symbol); org.w3c.dom.Text def = doc.createTextNode("DEF"); symbol.appendChild(def); soapHeader.appendChild(getLastTradePrice); assertEquals(1, soapHeader.getChildNodes().getLength()); Iterator iter = soapHeader.getChildElements(); assertTrue(iter.hasNext()); Object obj = iter.next(); // must be SOAPHeaderElement assertTrue(obj instanceof SOAPHeaderElement); SOAPElement soapElement = (SOAPElement) obj; assertEquals(namespace, soapElement.getNamespaceURI()); assertEquals(localName, soapElement.getLocalName()); iter = soapElement.getChildElements(); assertTrue(iter.hasNext()); obj = iter.next(); assertTrue(obj instanceof SOAPElement); soapElement = (SOAPElement) obj; assertEquals(null, soapElement.getNamespaceURI()); assertEquals("symbol", soapElement.getLocalName()); assertFalse(iter.hasNext()); iter = soapElement.getChildElements(); assertTrue(iter.hasNext()); obj = iter.next(); assertTrue(obj instanceof Text); Text text = (Text) obj; assertEquals("DEF", text.getData()); assertFalse(iter.hasNext()); }
public static void copyChildNodes(Element target, SOAPElement source) { // easy way out: no child nodes to copy if (!source.hasChildNodes()) return; final boolean traceEnabled = log.isTraceEnabled(); // traverse child nodes Iterator childIt = source.getChildElements(); while (childIt.hasNext()) { Object child = childIt.next(); if (child instanceof SOAPElement) { copyChildElement(target, (SOAPElement) child); } else if (child instanceof Text) { Text childTextNode = (Text) child; String text = childTextNode.getValue(); target.appendChild(target.getOwnerDocument().createTextNode(text)); if (traceEnabled) log.trace("added text node: " + text); } else { log.debug("discarding child node: " + child); } } }