001: /*
002: * Copyright (c) 2005 Sun Microsystems, Inc. All rights reserved. U.S.
003: * Government Rights - Commercial software. Government users are subject
004: * to the Sun Microsystems, Inc. standard license agreement and
005: * applicable provisions of the FAR and its supplements. Use is subject
006: * to license terms.
007: *
008: * This distribution may include materials developed by third parties.
009: * Sun, Sun Microsystems, the Sun logo, Java and J2EE are trademarks
010: * or registered trademarks of Sun Microsystems, Inc. in the U.S. and
011: * other countries.
012: *
013: * Copyright (c) 2005 Sun Microsystems, Inc. Tous droits reserves.
014: *
015: * Droits du gouvernement americain, utilisateurs gouvernementaux - logiciel
016: * commercial. Les utilisateurs gouvernementaux sont soumis au contrat de
017: * licence standard de Sun Microsystems, Inc., ainsi qu'aux dispositions
018: * en vigueur de la FAR (Federal Acquisition Regulations) et des
019: * supplements a celles-ci. Distribue par des licences qui en
020: * restreignent l'utilisation.
021: *
022: * Cette distribution peut comprendre des composants developpes par des
023: * tierces parties. Sun, Sun Microsystems, le logo Sun, Java et J2EE
024: * sont des marques de fabrique ou des marques deposees de Sun
025: * Microsystems, Inc. aux Etats-Unis et dans d'autres pays.
026: */
027: package domexample;
028:
029: import javax.xml.parsers.DocumentBuilder;
030: import javax.xml.parsers.DocumentBuilderFactory;
031: import javax.xml.parsers.FactoryConfigurationError;
032: import javax.xml.parsers.ParserConfigurationException;
033: import javax.xml.soap.*;
034: import org.xml.sax.SAXException;
035: import org.xml.sax.SAXParseException;
036: import java.io.File;
037: import java.io.IOException;
038: import java.util.*;
039: import org.w3c.dom.Document;
040: import org.w3c.dom.DOMException;
041: import org.w3c.dom.NodeList;
042:
043: public class DOMExample {
044: static Document document;
045:
046: public static void main(String[] args) {
047: if (args.length != 1) {
048: System.err.println("Argument required: "
049: + "-Dxml-file=<filename>");
050: System.exit(1);
051: }
052:
053: DOMExample de = new DOMExample();
054:
055: document = null;
056:
057: DocumentBuilderFactory factory = DocumentBuilderFactory
058: .newInstance();
059: factory.setNamespaceAware(true);
060:
061: try {
062: DocumentBuilder builder = factory.newDocumentBuilder();
063: document = builder.parse(new File(args[0]));
064: } catch (SAXParseException spe) {
065: // Error generated by the parser
066: System.out.println("\n** Parsing error" + ", line "
067: + spe.getLineNumber() + ", uri "
068: + spe.getSystemId());
069: System.out.println(" " + spe.getMessage());
070:
071: // Use the contained exception, if any
072: Exception x = spe;
073:
074: if (spe.getException() != null) {
075: x = spe.getException();
076: }
077:
078: x.printStackTrace();
079: } catch (SAXException sxe) {
080: // Error generated during parsing)
081: Exception x = sxe;
082:
083: if (sxe.getException() != null) {
084: x = sxe.getException();
085: }
086:
087: x.printStackTrace();
088: } catch (ParserConfigurationException pce) {
089: // Parser with specified options can't be built
090: pce.printStackTrace();
091: } catch (IOException ioe) {
092: // I/O error
093: ioe.printStackTrace();
094: }
095:
096: try {
097: // Create message factory and SOAP factory
098: MessageFactory messageFactory = MessageFactory
099: .newInstance();
100: SOAPFactory soapFactory = SOAPFactory.newInstance();
101:
102: // Create a message
103: SOAPMessage message = messageFactory.createMessage();
104:
105: // Get the SOAP header from the message and remove it
106: SOAPHeader header = message.getSOAPHeader();
107: header.detachNode();
108:
109: // Get the SOAP body from the message
110: SOAPBody body = message.getSOAPBody();
111:
112: // Add the DOM document to the message body
113: SOAPBodyElement docElement = body.addDocument(document);
114:
115: message.saveChanges();
116:
117: // Get contents using SAAJ APIs
118: Iterator iter1 = body.getChildElements();
119: de.getContents(iter1, "");
120: } catch (Exception ex) {
121: ex.printStackTrace();
122: }
123: }
124:
125: // main
126:
127: /*
128: * Retrieves the contents of the elements recursively and
129: * displays them.
130: *
131: * @param iterator Iterator returned by getChildElements
132: * @param indent indentation to nest element display
133: */
134: public void getContents(Iterator iterator, String indent) {
135: while (iterator.hasNext()) {
136: Node node = (Node) iterator.next();
137: SOAPElement element = null;
138: Text text = null;
139:
140: if (node instanceof SOAPElement) {
141: element = (SOAPElement) node;
142:
143: Name name = element.getElementName();
144: System.out.println(indent + "Name is "
145: + name.getQualifiedName());
146:
147: Iterator attrs = element.getAllAttributes();
148:
149: while (attrs.hasNext()) {
150: Name attrName = (Name) attrs.next();
151: System.out.println(indent + " Attribute name is "
152: + attrName.getQualifiedName());
153: System.out.println(indent + " Attribute value is "
154: + element.getAttributeValue(attrName));
155: }
156:
157: Iterator iter2 = element.getChildElements();
158: getContents(iter2, indent + " ");
159: } else {
160: text = (Text) node;
161:
162: String content = text.getValue();
163: System.out.println(indent + "Content is: " + content);
164: }
165: }
166: }
167: }
|