KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > domexample > DOMSrcExample


1 /*
2  * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. U.S.
3  * Government Rights - Commercial software. Government users are subject
4  * to the Sun Microsystems, Inc. standard license agreement and
5  * applicable provisions of the FAR and its supplements. Use is subject
6  * to license terms.
7  *
8  * This distribution may include materials developed by third parties.
9  * Sun, Sun Microsystems, the Sun logo, Java and J2EE are trademarks
10  * or registered trademarks of Sun Microsystems, Inc. in the U.S. and
11  * other countries.
12  *
13  * Copyright (c) 2005 Sun Microsystems, Inc. Tous droits reserves.
14  *
15  * Droits du gouvernement americain, utilisateurs gouvernementaux - logiciel
16  * commercial. Les utilisateurs gouvernementaux sont soumis au contrat de
17  * licence standard de Sun Microsystems, Inc., ainsi qu'aux dispositions
18  * en vigueur de la FAR (Federal Acquisition Regulations) et des
19  * supplements a celles-ci. Distribue par des licences qui en
20  * restreignent l'utilisation.
21  *
22  * Cette distribution peut comprendre des composants developpes par des
23  * tierces parties. Sun, Sun Microsystems, le logo Sun, Java et J2EE
24  * sont des marques de fabrique ou des marques deposees de Sun
25  * Microsystems, Inc. aux Etats-Unis et dans d'autres pays.
26  */

27 package domexample;
28
29 import javax.xml.parsers.DocumentBuilder JavaDoc;
30 import javax.xml.parsers.DocumentBuilderFactory JavaDoc;
31 import javax.xml.parsers.FactoryConfigurationError JavaDoc;
32 import javax.xml.parsers.ParserConfigurationException JavaDoc;
33 import javax.xml.soap.*;
34 import org.xml.sax.SAXException JavaDoc;
35 import org.xml.sax.SAXParseException JavaDoc;
36 import java.io.File JavaDoc;
37 import java.io.IOException JavaDoc;
38 import java.util.*;
39 import org.w3c.dom.Document JavaDoc;
40 import org.w3c.dom.DOMException JavaDoc;
41 import org.w3c.dom.NodeList JavaDoc;
42 import javax.xml.transform.dom.DOMSource JavaDoc;
43
44
45 public class DOMSrcExample {
46     static DOMSource JavaDoc domSource;
47
48     public static void main(String JavaDoc[] args) {
49         if (args.length != 1) {
50             System.err.println("Argument required: " + "-Dxml-file=<filename>");
51             System.exit(1);
52         }
53
54         DOMSrcExample dse = new DOMSrcExample();
55
56         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
57         factory.setNamespaceAware(true);
58
59         try {
60             DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
61             Document JavaDoc document = builder.parse(new File JavaDoc(args[0]));
62             domSource = new DOMSource JavaDoc(document);
63         } catch (SAXParseException JavaDoc spe) {
64             // Error generated by the parser
65
System.out.println("\n** Parsing error" + ", line " +
66                 spe.getLineNumber() + ", uri " + spe.getSystemId());
67             System.out.println(" " + spe.getMessage());
68
69             // Use the contained exception, if any
70
Exception JavaDoc x = spe;
71
72             if (spe.getException() != null) {
73                 x = spe.getException();
74             }
75
76             x.printStackTrace();
77         } catch (SAXException JavaDoc sxe) {
78             // Error generated during parsing)
79
System.out.println("\n** SAXException:");
80
81             Exception JavaDoc x = sxe;
82
83             if (sxe.getException() != null) {
84                 x = sxe.getException();
85             }
86
87             x.printStackTrace();
88         } catch (ParserConfigurationException JavaDoc pce) {
89             // Parser with specified options can't be built
90
System.out.println("\n** ParserConfigurationException:");
91             pce.printStackTrace();
92         } catch (IOException JavaDoc ioe) {
93             // I/O error
94
System.out.println("\n** IOException:");
95             ioe.printStackTrace();
96         }
97
98         try {
99             // Create message factory
100
MessageFactory messageFactory = MessageFactory.newInstance();
101
102             // Create a message
103
SOAPMessage message = messageFactory.createMessage();
104
105             // Get the SOAP part and set its content to domSource
106
SOAPPart soapPart = message.getSOAPPart();
107             soapPart.setContent(domSource);
108
109             message.saveChanges();
110
111             // Get contents using SAAJ APIs.
112
// Header is optional.
113
SOAPHeader header = message.getSOAPHeader();
114
115             if (header != null) {
116                 Iterator iter1 = header.getChildElements();
117                 System.out.println("Header contents:");
118                 dse.getContents(iter1, "");
119             }
120
121             SOAPBody body = message.getSOAPBody();
122             Iterator iter2 = body.getChildElements();
123             System.out.println("Body contents:");
124             dse.getContents(iter2, "");
125         } catch (Exception JavaDoc ex) {
126             System.out.println("\n** Exception:");
127             ex.printStackTrace();
128         }
129     }
130      // main
131

132     /*
133      * Retrieves the contents of the elements recursively and
134      * displays them.
135      *
136      * @param iterator Iterator returned by getChildElements
137      * @param indent indentation to nest element display
138      */

139     public void getContents(Iterator iterator, String JavaDoc indent) {
140         while (iterator.hasNext()) {
141             Node node = (Node) iterator.next();
142             SOAPElement element = null;
143             Text text = null;
144
145             if (node instanceof SOAPElement) {
146                 element = (SOAPElement) node;
147
148                 Name name = element.getElementName();
149                 System.out.println(indent + "Name is " +
150                     name.getQualifiedName());
151
152                 Iterator attrs = element.getAllAttributes();
153
154                 while (attrs.hasNext()) {
155                     Name attrName = (Name) attrs.next();
156                     System.out.println(indent + " Attribute name is " +
157                         attrName.getQualifiedName());
158                     System.out.println(indent + " Attribute value is " +
159                         element.getAttributeValue(attrName));
160                 }
161
162                 Iterator iter2 = element.getChildElements();
163                 getContents(iter2, indent + " ");
164             } else {
165                 text = (Text) node;
166
167                 String JavaDoc content = text.getValue();
168                 System.out.println(indent + "Content is: " + content);
169             }
170         }
171     }
172 }
173
Popular Tags