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: import javax.xml.transform.dom.DOMSource;
043:
044: public class DOMSrcExample {
045: static DOMSource domSource;
046:
047: public static void main(String[] args) {
048: if (args.length != 1) {
049: System.err.println("Argument required: "
050: + "-Dxml-file=<filename>");
051: System.exit(1);
052: }
053:
054: DOMSrcExample dse = new DOMSrcExample();
055:
056: DocumentBuilderFactory factory = DocumentBuilderFactory
057: .newInstance();
058: factory.setNamespaceAware(true);
059:
060: try {
061: DocumentBuilder builder = factory.newDocumentBuilder();
062: Document document = builder.parse(new File(args[0]));
063: domSource = new DOMSource(document);
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: System.out.println("\n** SAXException:");
082:
083: Exception x = sxe;
084:
085: if (sxe.getException() != null) {
086: x = sxe.getException();
087: }
088:
089: x.printStackTrace();
090: } catch (ParserConfigurationException pce) {
091: // Parser with specified options can't be built
092: System.out.println("\n** ParserConfigurationException:");
093: pce.printStackTrace();
094: } catch (IOException ioe) {
095: // I/O error
096: System.out.println("\n** IOException:");
097: ioe.printStackTrace();
098: }
099:
100: try {
101: // Create message factory
102: MessageFactory messageFactory = MessageFactory
103: .newInstance();
104:
105: // Create a message
106: SOAPMessage message = messageFactory.createMessage();
107:
108: // Get the SOAP part and set its content to domSource
109: SOAPPart soapPart = message.getSOAPPart();
110: soapPart.setContent(domSource);
111:
112: message.saveChanges();
113:
114: // Get contents using SAAJ APIs.
115: // Header is optional.
116: SOAPHeader header = message.getSOAPHeader();
117:
118: if (header != null) {
119: Iterator iter1 = header.getChildElements();
120: System.out.println("Header contents:");
121: dse.getContents(iter1, "");
122: }
123:
124: SOAPBody body = message.getSOAPBody();
125: Iterator iter2 = body.getChildElements();
126: System.out.println("Body contents:");
127: dse.getContents(iter2, "");
128: } catch (Exception ex) {
129: System.out.println("\n** Exception:");
130: ex.printStackTrace();
131: }
132: }
133:
134: // main
135:
136: /*
137: * Retrieves the contents of the elements recursively and
138: * displays them.
139: *
140: * @param iterator Iterator returned by getChildElements
141: * @param indent indentation to nest element display
142: */
143: public void getContents(Iterator iterator, String indent) {
144: while (iterator.hasNext()) {
145: Node node = (Node) iterator.next();
146: SOAPElement element = null;
147: Text text = null;
148:
149: if (node instanceof SOAPElement) {
150: element = (SOAPElement) node;
151:
152: Name name = element.getElementName();
153: System.out.println(indent + "Name is "
154: + name.getQualifiedName());
155:
156: Iterator attrs = element.getAllAttributes();
157:
158: while (attrs.hasNext()) {
159: Name attrName = (Name) attrs.next();
160: System.out.println(indent + " Attribute name is "
161: + attrName.getQualifiedName());
162: System.out.println(indent + " Attribute value is "
163: + element.getAttributeValue(attrName));
164: }
165:
166: Iterator iter2 = element.getChildElements();
167: getContents(iter2, indent + " ");
168: } else {
169: text = (Text) node;
170:
171: String content = text.getValue();
172: System.out.println(indent + "Content is: " + content);
173: }
174: }
175: }
176: }
|