GeeCON Prague 2014*
The moose likes Web Services and the fly likes From .Net DataTable to Java which object? Big Moose Saloon
  Search | Java FAQ | Recent Topics | Flagged Topics | Hot Topics | Zero Replies
Register / Login


JavaRanch » Java Forums » Java » Web Services
Bookmark "From .Net DataTable to Java which object?" Watch "From .Net DataTable to Java which object?" New topic
Author

From .Net DataTable to Java which object?

Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
I don't know C#/.Net but the C# code I want to communicate with returns a DataTable. I'm thinking ResultSet but I'm not sure... Does anybody here have a clue as to which reference type I should set the call to? Thanks!


SCJP 1.5
http://devpinoy.org/blogs/lamia/ - http://everypesocounts.com/
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2922
    
    5
Originally posted by Timothy Sam:
Does anybody here have a clue as to which reference type I should set the call to?


There isn't one. Non-.NET platforms have no clue what a DataTable is. Visual Studio generates a new XML schema for each DataTable type exposed through a Web Service. Which basically means that a non-.NET client has to pull the relevant information out of that incomprehensible mess. You can try to run a WSDL2Java tool on the published WSDL to see if you can make any sense of it.

Ideally the web service should translate the objects to an XML representation that its consistent with its business domain and not let any of the object implementation model leak through.

Web services exchange XML messages (which are hierarchal), not objects (which are object-oriented). Have a look at Tech Talk: Ted Neward on Web Services and Security

Start from your code, just sprinkle some web service magic pixie dust on it and lo and behold you have a web service, bad things, bad, bad, bad, bad. I need to beat the vendors over the head to stop doing that, to instead make tools, build tools that want to look at and see and recognize that (A) this is a message and (B) it�s in XML because wanting to take your code and seamlessly turning it into objects and back again, we�ve already talked about that. That�s just not going to work. It�s not going to happen. Once we beat the vendors into line, then it�s going out to the development community and saying, �Okay, here we have tools that support the idea of looking at this from a message oriented perspective, don�t fight that, don�t try to build RPC scenarios out of this messaging oriented approach�.
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
Hmmm... I think that got me a little more confused there... Does this mean I have to parse manually?
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2922
    
    5
Not necessarily. The first step is to design the Web services interface - i.e. WSDL (which you can create manually or with some sort of tool(like XMLSpy)). Then on the .NET side you can use Visual Studio to create the server stub from the WSDL. Then you write the glue code to move the data between your .NET object implementation model and the server stub. On the Java side you can use a WSDL2Java tool to create a client stub - then you write the glue code to move the data between the stub and the Java object implementation model.

Now in some more complicated cases some additional help is useful. There is Web Services Contract First on the .NET side. On the Java side JAXB is handy to transform your Java objects to and from XML - there is a lot of flexibility in the transformation process which is controlled through configuration.
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
Hi! Thanks for your effort to answer. I've generated some Java code using the WSDL2Java tool. However, an Exception is appearing now...



java.io.FileNotFoundException: C:\Program Files\netbeans-5.5\enterprise3\apache-tomcat-5.5.17\bin\org.tempuri.TimServiceSoapStub@fe0ce1 (The system cannot find the file specified)
at java.io.FileInputStream.open(Native Method)
at java.io.FileInputStream.<init>(FileInputStream.java:106)
at java.io.FileInputStream.<init>(FileInputStream.java:66)
at sun.net.www.protocol.file.FileURLConnection.connect(FileURLConnection.java:70)
at sun.net.www.protocol.file.FileURLConnection.getInputStream(FileURLConnection.java:161)
at org.apache.xerces.impl.XMLEntityManager.setupCurrentEntity(Unknown Source)
at org.apache.xerces.impl.XMLVersionDetector.determineDocVersion(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XML11Configuration.parse(Unknown Source)
at org.apache.xerces.parsers.XMLParser.parse(Unknown Source)
at org.apache.xerces.parsers.DOMParser.parse(Unknown Source)
at org.apache.xerces.jaxp.DocumentBuilderImpl.parse(Unknown Source)
at javax.xml.parsers.DocumentBuilder.parse(DocumentBuilder.java:180)
at com.wp.wpinventory.controller.FixedItemController.getEmployeeIds(FixedItemController.java:303)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at com.sun.faces.el.PropertyResolverImpl.getValue(PropertyResolverImpl.java:79)
at com.sun.faces.el.impl.ArraySuffix.evaluate(ArraySuffix.java:167)
at com.sun.faces.el.impl.ComplexValue.evaluate(ComplexValue.java:151)
at com.sun.faces.el.impl.ExpressionEvaluatorImpl.evaluate(ExpressionEvaluatorImpl.java:243)
at com.sun.faces.el.ValueBindingImpl.getValue(ValueBindingImpl.java:173)
at com.sun.faces.el.ValueBindingImpl.getValue(ValueBindingImpl.java:154)
at javax.faces.component.UISelectItems.getValue(UISelectItems.java:110)
at com.sun.faces.util.Util.getSelectItems(Util.java:600)
at com.sun.faces.renderkit.html_basic.MenuRenderer.getOptionNumber(MenuRenderer.java:488)
at com.sun.faces.renderkit.html_basic.MenuRenderer.renderSelect(MenuRenderer.java:465)
at com.sun.faces.renderkit.html_basic.MenuRenderer.encodeEnd(MenuRenderer.java:430)
at javax.faces.component.UIComponentBase.encodeEnd(UIComponentBase.java:720)
at com.sun.faces.renderkit.html_basic.HtmlBasicRenderer.encodeRecursive(HtmlBasicRenderer.java:443)
at com.sun.faces.renderkit.html_basic.GridRenderer.encodeChildren(GridRenderer.java:233)
at javax.faces.component.UIComponentBase.encodeChildren(UIComponentBase.java:701)
at javax.faces.webapp.UIComponentTag.encodeChildren(UIComponentTag.java:607)
at javax.faces.webapp.UIComponentTag.doEndTag(UIComponentTag.java:544)
at com.sun.faces.taglib.html_basic.PanelGridTag.doEndTag(PanelGridTag.java:460)
at org.apache.jsp.fixedItem.New_jsp._jspx_meth_h_panelGrid_0(New_jsp.java:369)
at org.apache.jsp.fixedItem.New_jsp._jspx_meth_h_form_0(New_jsp.java:195)
at org.apache.jsp.fixedItem.New_jsp._jspx_meth_f_view_0(New_jsp.java:143)
at org.apache.jsp.fixedItem.New_jsp._jspService(New_jsp.java:103)
at org.apache.jasper.runtime.HttpJspBase.service(HttpJspBase.java:97)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.jasper.servlet.JspServletWrapper.service(JspServletWrapper.java:332)
at org.apache.jasper.servlet.JspServlet.serviceJspFile(JspServlet.java:314)
at org.apache.jasper.servlet.JspServlet.service(JspServlet.java:264)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:802)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:368)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.ApplicationDispatcher.invoke(ApplicationDispatcher.java:672)
at org.apache.catalina.core.ApplicationDispatcher.processRequest(ApplicationDispatcher.java:463)
at org.apache.catalina.core.ApplicationDispatcher.doForward(ApplicationDispatcher.java:398)
at org.apache.catalina.core.ApplicationDispatcher.forward(ApplicationDispatcher.java:301)
at com.sun.faces.context.ExternalContextImpl.dispatch(ExternalContextImpl.java:322)
at com.sun.faces.application.ViewHandlerImpl.renderView(ViewHandlerImpl.java:130)
at com.sun.faces.lifecycle.RenderResponsePhase.execute(RenderResponsePhase.java:87)
at com.sun.faces.lifecycle.LifecycleImpl.phase(LifecycleImpl.java:200)
at com.sun.faces.lifecycle.LifecycleImpl.render(LifecycleImpl.java:117)
at javax.faces.webapp.FacesServlet.service(FacesServlet.java:198)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:252)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:368)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:202)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:173)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:213)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:178)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:126)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:105)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:107)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:148)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:869)
at org.apache.coyote.http11.Http11BaseProtocol$Http11ConnectionHandler.processConnection(Http11BaseProtocol.java:664)
at org.apache.tomcat.util.net.PoolTcpEndpoint.processSocket(PoolTcpEndpoint.java:527)
at org.apache.tomcat.util.net.LeaderFollowerWorkerThread.runIt(LeaderFollowerWorkerThread.java:80)
at org.apache.tomcat.util.threads.ThreadPool$ControlRunnable.run(ThreadPool.java:684)
at java.lang.Thread.run(Thread.java:619)



This is the code that generates the exception:



What does this error mean? Thanks!
michael warren
Ranch Hand

Joined: Oct 20, 2006
Posts: 50
From the stack trace I'm guessing the exception is from the line


The parse method that takes a String takes a URI (e.g. path to a file), I guess soapResponse.toString() isn't a valid URI.
Although I don't know what the TimServiceSoap class toString method does I'd make a guess that it returns the XML in the SOAP message ? in which case I think creating a StringBufferInputStream from it and passing that to the parse method that takes an InputStream would work.
Timothy Sam
Ranch Hand

Joined: Sep 18, 2005
Posts: 746
Hmmmm... Problem. I think the TimServiceSoap.toString() doesn't return the XML SOAP message as I thought it would... I tried printing in the console and gave me

org.tempuri.TimServiceSoapStub@1881bb1


this is the content of TimServiceSoap



and TimService




it would seem to me that I first need to get the XML from the SOAP message. How do I do that? Thanks for the clue!
Peer Reynders
Bartender

Joined: Aug 19, 2005
Posts: 2922
    
    5

org.tempuri.TimServiceSoapStub@1881bb1

That is the standard Java toString implementation:

packagename.classname@instanceID

it would seem to me that I first need to get the XML from the SOAP message. How do I do that?


Why would you want to? You should obtain an object implementing the service interface (TimService) from the stub. Your service interface has methods that return an object that implements the TimServiceSoap interface. The TimServiceSoap interface has two methods:
  • listEmployees() returning ListEmployeesResponseListEmployeesResult
  • listDepartments() returning ListDepartmentsResponseListDepartmentsResult

  • Use those instead. WSDL-to-Java hides all the SOAP-stuff in the generated code.
    See Creating Web Services with Apache Axis 3. WSDL2Java: Generate the Server-side Wrapper Code and Stubs For Easy Client Access for a simple example.

    You will want to change the default namespace of tempuri.org (temporary URI) that tools like visual studio put into XML documents like the WSDL - otherwise you could run into some bizarre errors due to naming collisions.
    [ May 16, 2007: Message edited by: Peer Reynders ]
    Timothy Sam
    Ranch Hand

    Joined: Sep 18, 2005
    Posts: 746
    Hi! Thanks! I already seem to be able to get the Nodes. However, the nodes doesn't their correct values. I can retrieve the right number of nodes from the service(by comparing it to the XML output in the .Net service) and the number is the same. The only difference is the the Node values are null... The XML output from the XML would be something like this...



    <?xml version="1.0" encoding="utf-8" ?>
    - <DataTable xmlns="http://tempuri.org/">
    - <xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
    - <xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:MainDataTable="Employees" msdata:UseCurrentLocale="true">
    - <xs:complexType>
    - <xs:choice minOccurs="0" maxOccurs="unbounded">
    - <xs:element name="Employees">
    - <xs:complexType>
    - <xs:sequence>
    <xs:element name="empid" type="xs:int" minOccurs="0" />
    <xs:element name="firstname" type="xs:string" minOccurs="0" />
    <xs:element name="middlename" type="xs:string" minOccurs="0" />
    <xs:element name="lastname" type="xs:string" minOccurs="0" />
    </xs:sequence>
    </xs:complexType>
    </xs:element>
    </xs:choice>
    </xs:complexType>
    </xs:element>
    </xs:schema>
    - <diffgr iffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns iffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
    - <DocumentElement xmlns="">
    - <Employees diffgr:id="Employees1" msdata:rowOrder="0">
    <empid>2</empid>
    <firstname>Timothy</firstname>
    <middlename>Whatever</middlename>
    <lastname>Sam</lastname>
    </Employees>
    - <Employees diffgr:id="Employees2" msdata:rowOrder="0">
    <empid>3</empid>
    <firstname>Some</firstname>
    <middlename>Other</middlename>
    <lastname>Person</lastname>
    </Employees>

    </DocumentElement>
    </diffgr iffgram>
    </DataTable>





    and now with my method...




    the getNodeValue() just return NULL. The getNodeName() however does return the correct node name... Sample output would be like this:



    Node: Employees
    Node[0]: [Employees: null]
    Child:empid
    Child:null
    Child:firstname
    Child:null
    Child:middlename
    Child:null
    Child:lastname
    Child:null
    Node: Employees
    Node[1]: [Employees: null]
    Child:empid
    Child:null
    Child:firstname
    Child:null
    Child:middlename
    Child:null
    Child:lastname
    Child:null


    I'm using the following libraries for Document and XML parsing stuff...

    import org.w3c.dom.Document;
    import org.w3c.dom.NodeList;
    import org.w3c.dom.Node;

    and ofcourse the MessageElement class

    import org.apache.axis.message.MessageElement;


    Hmmm... What could be wrong? Thanks!
    [ May 17, 2007: Message edited by: Timothy Sam ]
    Timothy Sam
    Ranch Hand

    Joined: Sep 18, 2005
    Posts: 746
    Solved it! I just used the getTextContent() method instead. Weeeee! Thank you guys for all your help! I'll be posting the solution on my Blog so keep posted.
    Peer Reynders
    Bartender

    Joined: Aug 19, 2005
    Posts: 2922
        
        5
    Originally posted by Timothy Sam:
    Hmmm... What could be wrong? Thanks!


    Any non-XML text between the start tag and end tag of an ELEMENT_NODE goes into separate TEXT_NODEs which are children of the ELEMENT_NODE. (Remember XML can also be document-oriented. Web Services deal with data-oriented XML so some of the conventions may seem a bit odd). So you need to dig deeper.

    From: Chapter 12. The DOM Traversal Module
    The DOM 3 getTextContent() won�t work on Document objects (not that you care here).
    Processing XML with Java
    DOM
    [ May 17, 2007: Message edited by: Peer Reynders ]
     
    GeeCON Prague 2014
     
    subject: From .Net DataTable to Java which object?