I need to a create a java webservice which should take XML as input.
This XML is the output of another webservice service (output type in
the wsdl is base64Binary). What should be input parameter type to take
a XML into the Java web service.
I tried to use javax.transform.Source. This gives me error while
deployment.
robert - 22 Feb 2006 20:39 GMT
> I need to a create a java webservice which should take XML as input.
> This XML is the output of another webservice service (output type in
[quoted text clipped - 3 lines]
> I tried to use javax.transform.Source. This gives me error while
> deployment.
Base64 in wsdl maps to java as byte[] . So convert the Document to a
string and then to bytes:
public static final String getDocumentAsString(Document document)
throws XMLHelperException {
try {
// Create source and result objects
Source source = new DOMSource(document);
StringWriter out = new StringWriter();
Result result = new StreamResult(out);
TransformerFactory tFactory =
TransformerFactory.newInstance();
Transformer transformer = tFactory.newTransformer();
transformer.transform(source, result);
return out.toString();
} catch (Exception ex) {
throw new XMLHelperException("XML Document to String
Error", ex);
}
}
Just place in a line:
out.toString().getBytes(); and change the return type to byte[] .
Re-reading your post I think you may need to do the reverse - byte[] to
document. That would be:
new String (out.toString().getBytes());
IOW, create a new string with the byte array. Then:
public static final Document getDocument(String xmlString)
throws XMLHelperException {
try {
String nstr = null;
//cannot have whitespace in the beginning of an xml
document
if (xmlString.charAt(0) != ' ') {
nstr = removeInitialWS(xmlString);
} else {
nstr = xmlString;
}
DocumentBuilderFactory factory =
DocumentBuilderFactory.newInstance();
factory.setNamespaceAware(true);
factory.setIgnoringElementContentWhitespace(false);
DocumentBuilder builder = factory.newDocumentBuilder();
InputSource isXml = new InputSource(new
StringReader(nstr));
return builder.parse(isXml);
} catch (Exception ex) {
throw new XMLHelperException("String to XML Document Error
for "
+ "String:\n\n " + xmlString + " ", ex);
}
}
HTH,
iksrazal
http://www.braziloutsource.com/
Anand B - 22 Feb 2006 21:43 GMT
tom fredriksen - 22 Feb 2006 22:15 GMT
> I need to a create a java webservice which should take XML as input.
> This XML is the output of another webservice service (output type in
> the wsdl is base64Binary). What should be input parameter type to take
> a XML into the Java web service.
Either you base64 code it, specify it as a XMLString type or you can use
a CDATA enclosement. I prefer the latter one is it will not create any
more problems, which the others possibly can do.
Tip:
XMLString will become a much larger string than CDATA because everything
that is xml syntax must be escaped.
/tom