You are viewing a plain text version of this content. The canonical link for it is here.
Posted to java-user@axis.apache.org by "KUMAR,PANKAJ (HP-Cupertino,ex1)" <pa...@hp.com> on 2003/04/14 20:06:37 UTC

RE: How to convert a org.w3c.dom.Element to javax.xml.soap.SOAPEl emen t?

Done. Find the Bug Report at:
http://nagoya.apache.org/bugzilla/show_bug.cgi?id=19008

/Pankaj.

> -----Original Message-----
> From: Davanum Srinivas [mailto:dims@yahoo.com]
> Sent: Monday, April 14, 2003 5:06 AM
> To: axis-user@ws.apache.org
> Subject: RE: How to convert a org.w3c.dom.Element to
> javax.xml.soap.SOAPEl emen t?
> 
> 
> Pankaj,
> 
> :) :) Please log a bugzilla bug 
> (http://nagoya.apache.org/bugzilla) and one of us (committers)
> will take care of it.
> 
> Thanks,
> dims
> 
> --- "KUMAR,PANKAJ (HP-Cupertino,ex1)" <pa...@hp.com> wrote:
> > Hi Dims,
> > 
> > Thanks (you never fail me !!). This one had not struck me 
> earlier. But read
> > on.
> > 
> > BTW, I had written a small utility class to do the 
> conversion (SOAPMessage
> > --> Document and Document --> SOAPMessage; assuming that 
> there were no
> > attachments) for me. This class essentially wrote the 
> SOAPMessage to a
> > bytestream (using writeTo() method and then parsed the 
> bytestream to get a
> > Document. I used a similar technique other way round as well.
> > 
> > After getting your mail, I thought I will use 
> getContent/setContent() of
> > SOAPPart to do the conversion. So I changed my 
> implementation. But then ran
> > into a ClassCastException problem. Some digging into Axis 
> source code
> > revealed that following code, in file XMLUtils.java, is 
> causing problem:
> > 
> >     public static InputSource sourceToInputSource(Source source) {
> > 		// skip
> >             Element domElement = 
> (Element)((DOMSource)source).getNode();
> > 		// skip
> >     }
> > 
> > In case the DOMSource is created with a Document, getNode() 
> doesn't return
> > an Element. This is certainly a bug in Axis code.
> > 
> > Here is my test code (in case someone wants to reproduce 
> the problem ):
> > Note: My original code is comented out.
> > 
> --------------------------------------------------------------
> --------------
> > -----
> > // File: SOAPUtility.java
> > import java.io.ByteArrayInputStream;
> > import java.io.ByteArrayOutputStream;
> > import java.io.IOException;
> > 
> > import javax.xml.soap.MessageFactory;
> > import javax.xml.soap.SOAPMessage;
> > import javax.xml.soap.MimeHeaders;
> > import javax.xml.soap.SOAPException;
> > 
> > import javax.xml.parsers.DocumentBuilderFactory;
> > import javax.xml.parsers.DocumentBuilder;
> > import javax.xml.parsers.ParserConfigurationException;
> > import javax.xml.transform.TransformerFactory;
> > import javax.xml.transform.Transformer;
> > import javax.xml.transform.Source;
> > import javax.xml.transform.dom.DOMSource;
> > import javax.xml.transform.dom.DOMResult;
> > import javax.xml.transform.stream.StreamResult;
> > import javax.xml.transform.TransformerConfigurationException;
> > import javax.xml.transform.TransformerException;
> > import org.xml.sax.SAXException;
> > import org.w3c.dom.Document;
> > 
> > 
> > public class SOAPUtility {
> > 	
> > 
> /*************************************************************
> **************
> > *
> > 	 * class MyByteArrayOutputStream helps in avoiding a copy of the
> > buffer.
> > 	 * Method getByteArrayInputStream() can access the 
> protected members
> > buf and count
> > 	 * of the base class, thus passing the same buffer to the
> > ByteArrayInputStream.
> > 	
> > 
> **************************************************************
> **************
> > /
> > 	private static class MyByteArrayOutputStream extends
> > ByteArrayOutputStream {
> > 		public MyByteArrayOutputStream(){
> > 			super();
> > 		}
> > 		public ByteArrayInputStream getByteArrayInputStream(){
> > 			return new ByteArrayInputStream(buf, 0, count);
> > 		}
> > 	}
> > 
> > /*
> > 	public static Document toDocument(SOAPMessage soapMsg) throws
> > 				ParserConfigurationException, 
> SAXException,
> > SOAPException, IOException {
> > 		MyByteArrayOutputStream baos = new
> > MyByteArrayOutputStream();
> > 		soapMsg.writeTo(baos);
> > 		ByteArrayInputStream bais = 
> baos.getByteArrayInputStream();
> > 
> >       	DocumentBuilderFactory dbf = 
> DocumentBuilderFactory.newInstance();
> > 		dbf.setNamespaceAware(true);
> > 		DocumentBuilder db = dbf.newDocumentBuilder();
> > 		Document doc = db.parse(bais);
> > 		return doc;
> > 	}
> > */
> > 	public static Document toDocument(SOAPMessage soapMsg) throws
> > 				TransformerConfigurationException,
> > TransformerException, SOAPException, IOException {
> > 		Source src = soapMsg.getSOAPPart().getContent();
> > 		TransformerFactory tf = 
> TransformerFactory.newInstance();
> > 		Transformer transformer = tf.newTransformer();
> > 		DOMResult result = new DOMResult();
> > 		transformer.transform(src, result);
> > 		return (Document)result.getNode();
> > 	}
> > /*
> > 	public static SOAPMessage toSOAPMessage(Document doc) throws
> > 			TransformerConfigurationException,
> > TransformerException,  SOAPException, IOException {
> > 		TransformerFactory tf = 
> TransformerFactory.newInstance();
> > 		Transformer transformer = tf.newTransformer();
> > 		MyByteArrayOutputStream baos = new
> > MyByteArrayOutputStream();
> > 		transformer.transform(new DOMSource(doc), new
> > StreamResult(baos));
> > 		ByteArrayInputStream bais = 
> baos.getByteArrayInputStream();
> > 
> > 		MessageFactory mf = MessageFactory.newInstance();
> > 		SOAPMessage soapMsg = mf.createMessage(new 
> MimeHeaders(),
> > bais);
> > 		return soapMsg;
> > 	}
> > */
> > 	public static SOAPMessage toSOAPMessage(Document doc) throws
> > 			TransformerConfigurationException,
> > TransformerException,  SOAPException, IOException {
> > 		DOMSource src = new DOMSource(doc);
> > 
> > 		MessageFactory mf = MessageFactory.newInstance();
> > 		SOAPMessage soapMsg = mf.createMessage();
> > 		soapMsg.getSOAPPart().setContent(src);
> > 		return soapMsg;
> > 	}
> > 
> > 	public static void main(String[] args) throws Exception {
> > 		Document doc = XmlUtility.readXML("soap.xml");
> > 		System.out.println("Document read from file:");
> > 		XmlUtility.writeXML(doc, System.out);
> > 		System.out.println();
> > 
> > 		SOAPMessage soapMsg = toSOAPMessage(doc);
> > 		System.out.println("Document converted to 
> SOAPMessage:");
> > 		soapMsg.writeTo(System.out);
> > 		System.out.println();
> > 
> > 		doc = toDocument(soapMsg);
> > 		System.out.println("SOAPMessage converted to 
> Document:");
> > 		XmlUtility.writeXML(doc, System.out);
> > 		System.out.println();
> > 	}
> > }
> > 
> --------------------------------------------------------------
> --------------
> > -----
> > Thanks,
> > Pankaj Kumar.
> > > -----Original Message-----
> > > From: Davanum Srinivas [mailto:dims@yahoo.com]
> > > Sent: Sunday, April 13, 2003 7:09 AM
> > > To: axis-user@ws.apache.org
> > > Subject: Re: How to convert a org.w3c.dom.Element to
> > > javax.xml.soap.SOAPElemen t?
> > > 
> > > 
> > > http://java.sun.com/webservices/docs/1.1/api/javax/xml/soap/SO
> > > APPart.html#setContent(javax.xml.transform.Source)
> > > 
> > > --- "KUMAR,PANKAJ (HP-Cupertino,ex1)" <pa...@hp.com> wrote:
> > > > Hi,
> > > > 
> > > > In my program I have a org.w3c.dom.Element object that I 
> > > want to make a
> > > > child of <Body> element within a SOAP Envelope. The 
> classes within
> > > > javax.xml.soap seem to require a SOAPElement object. SO, 
> > > how do I convert my
> > > > Element object to SOAPElement? Is the only solution to 
> > > iterate over the
> > > > children of Element and build the sub-tree of SOAPElement 
> > > objects? This
> > > > seems an expensive solution to me.
> > > > 
> > > > Any ideas?
> > > > 
> > > > Thanks,
> > > > Pankaj Kumar.
> > > > homepage: http://www.pankaj-k.net
> > > > 
> > > 
> > > 
> > > =====
> > > Davanum Srinivas - http://webservices.apache.org/~dims/
> > > 
> > > __________________________________________________
> > > Do you Yahoo!?
> > > Yahoo! Tax Center - File online, calculators, forms, and more
> > > http://tax.yahoo.com
> > > 
> 
> 
> =====
> Davanum Srinivas - http://webservices.apache.org/~dims/
> 
> __________________________________________________
> Do you Yahoo!?
> Yahoo! Tax Center - File online, calculators, forms, and more
> http://tax.yahoo.com
>