1 package test.saaj; 2 3 import org.w3c.dom.Document ; 4 import org.w3c.dom.Node ; 5 import org.w3c.dom.NamedNodeMap ; 6 import org.w3c.dom.Attr ; 7 import org.w3c.dom.Text ; 8 import org.w3c.dom.NodeList ; 9 10 import javax.xml.soap.MessageFactory ; 11 import javax.xml.soap.Name ; 12 import javax.xml.soap.SOAPBody ; 13 import javax.xml.soap.SOAPElement ; 14 import javax.xml.soap.SOAPHeader ; 15 import javax.xml.soap.SOAPMessage ; 16 import javax.xml.soap.SOAPFactory ; 17 import javax.xml.soap.SOAPBodyElement ; 18 import javax.xml.soap.SOAPEnvelope ; 19 import javax.xml.soap.SOAPPart ; 20 import javax.xml.parsers.DocumentBuilderFactory ; 21 import javax.xml.parsers.DocumentBuilder ; 22 import java.io.ByteArrayInputStream ; 23 24 public class TestText extends junit.framework.TestCase { 25 26 public TestText(String name) { 27 super(name); 28 } 29 30 public void testAddTextNode() throws Exception { 32 SOAPFactory soapFactory = SOAPFactory.newInstance(); 33 MessageFactory factory = MessageFactory.newInstance(); 34 SOAPMessage message = factory.createMessage(); 35 SOAPHeader header = message.getSOAPHeader(); 36 SOAPBody body = message.getSOAPBody(); 37 38 Name bodyName = soapFactory.createName("VBGenReceiver", "xsi", 40 "http://www.w3.org/2001/XMLSchema-instance"); 41 SOAPBodyElement bodyElement = body.addBodyElement(bodyName); 42 43 Name name = soapFactory.createName("MetaData"); 45 SOAPElement metaData = bodyElement.addChildElement(name); 46 47 name = soapFactory.createName("SKey"); 49 SOAPElement sKey = metaData.addChildElement(name); 50 sKey.addTextNode("SKEY001"); 51 52 name = soapFactory.createName("Object"); 54 SOAPElement object = bodyElement.addChildElement(name); 55 56 name = soapFactory.createName("BookID"); 58 SOAPElement bookID = object.addChildElement(name); 59 bookID.addTextNode("BookID002"); 60 61 name = soapFactory.createName("OrderID"); 63 SOAPElement orderID = object.addChildElement(name); 64 orderID.addTextNode("OrderID003"); 65 66 name = soapFactory.createName("PurchaseID"); 68 SOAPElement purchaseID = object.addChildElement(name); 69 purchaseID.addTextNode("PurchaseID005"); 70 71 name = soapFactory.createName("LanguageID"); 73 SOAPElement languageID = object.addChildElement(name); 74 languageID.addTextNode("LanguageID004"); 75 76 name = soapFactory.createName("LanguageName"); 78 SOAPElement languageName = object.addChildElement(name); 79 languageName.addTextNode("LanguageName006"); 80 81 name = soapFactory.createName("Title"); 83 SOAPElement title = object.addChildElement(name); 84 title.addTextNode("Title007"); 85 86 name = soapFactory.createName("Author"); 88 SOAPElement author = object.addChildElement(name); 89 author.addTextNode("Author008"); 90 91 name = soapFactory.createName("Format"); 93 SOAPElement format = bodyElement.addChildElement(name); 94 95 name = soapFactory.createName("Type"); 97 SOAPElement formatType = format.addChildElement(name); 98 formatType.addTextNode("Type009"); 99 100 name = soapFactory.createName("Delivery"); 102 SOAPElement delivery = bodyElement.addChildElement(name); 103 104 name = soapFactory.createName("Name"); 106 SOAPElement delName = delivery.addChildElement(name); 107 delName.addTextNode("Name010"); 108 109 name = soapFactory.createName("Address1"); 111 SOAPElement address1 = delivery.addChildElement(name); 112 address1.addTextNode("Address1011"); 113 114 name = soapFactory.createName("Address2"); 116 SOAPElement address2 = delivery.addChildElement(name); 117 address2.addTextNode("Address2012"); 118 119 name = soapFactory.createName("City"); 121 SOAPElement city = delivery.addChildElement(name); 122 city.addTextNode("City013"); 123 124 name = soapFactory.createName("State"); 126 SOAPElement state = delivery.addChildElement(name); 127 state.addTextNode("State014"); 128 129 name = soapFactory.createName("PostalCode"); 131 SOAPElement postalCode = delivery.addChildElement(name); 132 postalCode.addTextNode("PostalCode015"); 133 134 System.out.println("The message is lll:\n"); 135 message.writeTo(System.out); 136 } 137 138 public void testTraverseDOM() throws Exception { 139 String xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n" + 140 "<book year=\"1992\">\n" + 141 " <title>Advanced Programming in the Unix environment</title>\n" + 142 " <author><last>Stevens</last><first>W.</first></author>\n" + 143 " <publisher>Addison-Wesley</publisher>\n" + 144 " <price>65.95</price>\n" + 145 "</book>\n" + 146 ""; 147 148 DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance(); 149 factory.setNamespaceAware(true); 150 DocumentBuilder builder = factory.newDocumentBuilder(); 151 Document payload = builder.parse(new ByteArrayInputStream (xml.getBytes())); 152 153 MessageFactory soapMsgFactory = MessageFactory.newInstance(); 154 SOAPMessage soapMsg = soapMsgFactory.createMessage(); 155 156 SOAPPart soapPart = soapMsg.getSOAPPart(); 157 SOAPEnvelope soapEnv = soapPart.getEnvelope(); 158 SOAPBody soapBody = soapEnv.getBody(); 159 160 soapBody.addDocument(payload); 161 162 System.out.println("***************"); 163 soapMsg.writeTo(System.out); 164 165 processNode(soapPart); 166 } 167 168 private void processNode(Node currentNode) { 169 switch (currentNode.getNodeType()) { 170 case Node.DOCUMENT_NODE: 172 Document doc = (Document ) currentNode; 173 System.out.println("Document node: " + doc.getNodeName() + 174 "\nRoot element: " + 175 doc.getDocumentElement().getNodeName()); 176 processChildNodes(doc.getChildNodes()); 177 break; 178 179 case Node.ELEMENT_NODE: 181 System.out.println("\nElement node: " + 182 currentNode.getNodeName()); 183 NamedNodeMap attributeNodes = 184 currentNode.getAttributes(); 185 for (int i = 0; i < attributeNodes.getLength(); i++) { 186 Attr attribute = (Attr ) attributeNodes.item(i); 187 System.out.println("\tAttribute: " + 188 attribute.getNodeName() + " ; Value = " + 189 attribute.getNodeValue()); 190 } 191 processChildNodes(currentNode.getChildNodes()); 192 break; 193 194 case Node.CDATA_SECTION_NODE: 196 case Node.TEXT_NODE: 197 Text text = (Text ) currentNode; 198 if (!text.getNodeValue().trim().equals("")) 199 System.out.println("\tText: " + 200 text.getNodeValue()); 201 break; 202 } 203 } 204 205 private void processChildNodes(NodeList children) { 206 if (children.getLength() != 0) 207 for (int i = 0; i < children.getLength(); i++) 208 processNode(children.item(i)); 209 } 210 211 212 public static void main(String [] args) throws Exception { 213 TestText tester = new TestText("TestEnvelope"); 214 tester.testAddTextNode(); 215 } 216 } 217 | Popular Tags |