KickJava   Java API By Example, From Geeks To Geeks.

Java > Open Source Codes > domexample > DOMExample


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
43
44 public class DOMExample {
45     static Document JavaDoc document;
46
47     public static void main(String JavaDoc[] args) {
48         if (args.length != 1) {
49             System.err.println("Argument required: " + "-Dxml-file=<filename>");
50             System.exit(1);
51         }
52
53         DOMExample de = new DOMExample();
54
55         document = null;
56
57         DocumentBuilderFactory JavaDoc factory = DocumentBuilderFactory.newInstance();
58         factory.setNamespaceAware(true);
59
60         try {
61             DocumentBuilder JavaDoc builder = factory.newDocumentBuilder();
62             document = builder.parse(new File JavaDoc(args[0]));
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
Exception JavaDoc x = sxe;
80
81             if (sxe.getException() != null) {
82                 x = sxe.getException();
83             }
84
85             x.printStackTrace();
86         } catch (ParserConfigurationException JavaDoc pce) {
87             // Parser with specified options can't be built
88
pce.printStackTrace();
89         } catch (IOException JavaDoc ioe) {
90             // I/O error
91
ioe.printStackTrace();
92         }
93
94         try {
95             // Create message factory and SOAP factory
96
MessageFactory messageFactory = MessageFactory.newInstance();
97             SOAPFactory soapFactory = SOAPFactory.newInstance();
98
99             // Create a message
100
SOAPMessage message = messageFactory.createMessage();
101
102             // Get the SOAP header from the message and remove it
103
SOAPHeader header = message.getSOAPHeader();
104             header.detachNode();
105
106             // Get the SOAP body from the message
107
SOAPBody body = message.getSOAPBody();
108
109             // Add the DOM document to the message body
110
SOAPBodyElement docElement = body.addDocument(document);
111
112             message.saveChanges();
113
114             // Get contents using SAAJ APIs
115
Iterator iter1 = body.getChildElements();
116             de.getContents(iter1, "");
117         } catch (Exception JavaDoc ex) {
118             ex.printStackTrace();
119         }
120     }
121      // main
122

123     /*
124      * Retrieves the contents of the elements recursively and
125      * displays them.
126      *
127      * @param iterator Iterator returned by getChildElements
128      * @param indent indentation to nest element display
129      */

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