• Post Reply Bookmark Topic Watch Topic
  • New Topic

Return type recomendation

 
jite eghagha
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I've finished up my class that gets a resultSet, cleans up my data, builds a Documnet Object, and saves my "Data" as a .xml file using the Transformer class.

But, my goal is not to save the .xml file.

I want to return the "Data" to a client.

Here are two chunks of code to draw you a picture:

//Obtain default parser
DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
//Obtain objects that build documents
DocumentBuilder builder = factory.newDocumentBuilder();
//create Root node
document = builder.newDocument();
ResultSetMetaData metaData = rs.getMetaData();
root = document.createElement("root");
document.appendChild(root);
..........................................
StringToElement = rs.getString(4);
if ( StringToElement != null ) {
String str1ColumnName = metaData.getColumnName(4);
SubTaskElement = document.createElement(str1ColumnName);
SubTaskElement.appendChild(document.createTextNode(StringToElement));
SubTaskNode.appendChild(SubTaskElement);
root.appendChild(SubTaskNode);
}
saveXml(document); // at this point i thought "return type!!"


What is a suggested return type to send to a client ?
A .xml file ? (is this possible)
A Document Object ? (will all clients recognize the structure)
[ October 18, 2006: Message edited by: jite eghagha ]
 
Peer Reynders
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Have a look at 3.7.1 Exchanging XML Documents.
Basically the suggestion is to pass the (arbitrary) XML document as a string.

An alternative to consider: express the XML document(s) that you are creating through XML-Schema(s) and incorporate them in your web service's WSDL (as document-oriented endpoints; Patterns and Strategies for Building Document-Based Web Services). Then use a WSDL-to-Java tool to create your server stub. That way the XML-handling code is generated for you.
 
jite eghagha
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
I tried Option 1, returning a javax.xml.transform.Source object.

I use JAX WS so my web service looks like this.

@WebMethod
public Source getProjectRecordByID_XML(@WebParam(name ="pStrProjectID")String pStrProjectID)
{
DataBase DB = new DataBase();
DB.setInputString(pStrProjectID);
return DB.getXmlResult(eRequest.eGetProjectRecordByID);
}

but Source gets converted to a byte[].

So if i am to use a String my question changes.

option 2.
If i return a string? how do i convert a Document, or .xml file, or what ever into a String?

i'm still reading up the WSDL to JAVA; so for now i need a solution that doesn't involve my messing with my WSDL.
[ October 19, 2006: Message edited by: jite eghagha ]
 
Peer Reynders
Bartender
Posts: 2968
6
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
If the XML is already in a file, simply read it into a string like any normal text file.
Reading a text file

If its already in some parsed form you can use the JAXP XSLT API to help you transform it. The following is from J2EE Web Services (Richard Monson-Haefel; RMH):

You can hand in a ByteArrayOutputStream and extract the String from that.
[ October 19, 2006: Message edited by: Peer Reynders ]
 
jite eghagha
Ranch Hand
Posts: 128
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
THANKS,

I'm greatful.

eghagha
 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!