Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
HomeAnnouncementsWhite Papers
Discussion GroupsFirst AidDatabasesJavaBeansGUIJava 3DVirtual MachineCORBASecurityToolsGeneral
Java DirectoryOpen Source ProjectsSample Book ChaptersUser GroupsWeb Resources
Related Topics
Databases.NETMore Topics ...

Java Forum / General / February 2006

Tip: Looking for answers? Try searching our database.

Java DataType for XML for webservice

Thread view: 
Anand B - 22 Feb 2006 20:02 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.

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


Free Magazines

Get these publications absolutely FREE for up to 12 months. There are no hidden fees and no obligation. Simply choose a title, complete the application form and submit it. Read more ...

Oracle MagazineNetwork ComputingComputer WorldBio-IT WorldeWeekInformation WeekInfosecurity
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2009 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.