1 package test.saaj; 2 3 import org.apache.axis.utils.XMLUtils; 4 import org.w3c.dom.Document ; 5 import org.w3c.dom.Node ; 6 import test.AxisTestBase; 7 8 import javax.xml.parsers.DocumentBuilder ; 9 import javax.xml.parsers.DocumentBuilderFactory ; 10 import javax.xml.soap.MessageFactory ; 11 import javax.xml.soap.MimeHeaders ; 12 import javax.xml.soap.SOAPBody ; 13 import javax.xml.soap.SOAPMessage ; 14 import javax.xml.soap.SOAPPart ; 15 import java.io.ByteArrayInputStream ; 16 17 public class TestImport extends AxisTestBase { 18 19 public TestImport(String name) { 20 super(name); 21 } 22 23 private static final String SAMPLE_1 = 24 "<SOAP-ENV:Envelope xmlns:SOAP-ENV=\"http://schemas.xmlsoap.org/soap/envelope/\">" + 25 "\n" + 26 "<SOAP-ENV:Body> " + "\n" + 27 "<m:GetLastTradePrice xmlns:m=\"http://wombat.ztrade.com\">" + 28 "\n" + 29 "<symbol>SUNW</symbol> " + "\n" + 30 "</m:GetLastTradePrice> " + "\n" + 31 "</SOAP-ENV:Body> " + "\n" + 32 "</SOAP-ENV:Envelope>"; 33 34 private SOAPMessage getSOAPMessageFromString(String str) throws Exception { 35 MimeHeaders mimeHeaders = new MimeHeaders (); 36 mimeHeaders.addHeader("content-type", "text/xml"); 37 SOAPMessage message = MessageFactory.newInstance().createMessage( 38 mimeHeaders, 39 new ByteArrayInputStream (str.getBytes())); 40 return message; 41 } 42 43 public void testImports() throws Exception { 44 48 Document doc2 = testImportFromSaajToDom(); 49 Document body = testImportFromDomToSaaj(doc2); 50 XMLUtils.PrettyDocumentToStream(body, System.out); 51 } 55 56 private Document testImportFromSaajToDom() throws Exception { 57 SOAPMessage message = getSOAPMessageFromString(SAMPLE_1); 58 DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance(); 59 DocumentBuilder db = dbf.newDocumentBuilder(); 60 Document doc = db.newDocument(); 61 org.w3c.dom.Node fromNode = message.getSOAPBody().getFirstChild(); 62 Node n = doc.importNode(fromNode, true); 63 doc.appendChild(n); 64 return doc; 65 } 66 67 private Document testImportFromDomToSaaj(Document doc) throws Exception { 68 SOAPMessage sm = MessageFactory.newInstance().createMessage(); 69 SOAPPart sp = sm.getSOAPPart(); 70 SOAPBody body = sm.getSOAPBody(); 71 org.w3c.dom.Node node = sp.importNode(doc.getDocumentElement(), true); 72 body.appendChild(node); 73 return sp; 74 } 75 } 76 77 | Popular Tags |