title: Parsing a .NET DataSet returned from a .NET Web Service in Java
body: <p>I have to consume a .NET hosted web service from a Java application. Interoperability between the two is usually very good. The problem I'm running into is that the .NET application developer chose to expose data using the .NET DataSet object. There are lots of articles written as to why you should not do this and how it makes interoperability difficult:</p>
  
  <ul>
  <li><a href="http://www.hanselman.com/blog/ReturningDataSetsFromWebServicesIsTheSpawnOfSatanAndRepresentsAllThatIsTrulyEvilInTheWorld.aspx" rel="noreferrer">http://www.hanselman.com/blog/ReturningDataSetsFromWebServicesIsTheSpawnOfSatanAndRepresentsAllThatIsTrulyEvilInTheWorld.aspx</a></li>
  <li><a href="http://www.lhotka.net/weblog/ThoughtsOnPassingDataSetObjectsViaWebServices.aspx" rel="noreferrer">http://www.lhotka.net/weblog/ThoughtsOnPassingDataSetObjectsViaWebServices.aspx</a></li>
  <li><a href="https://web.archive.org/web/20210616111510/https://aspnet.4guysfromrolla.com/articles/051805-1.aspx" rel="noreferrer">https://web.archive.org/web/20210616111510/https://aspnet.4guysfromrolla.com/articles/051805-1.aspx</a></li>
  <li><a href="http://www.theserverside.net/tt/articles/showarticle.tss?id=Top5WSMistakes" rel="noreferrer">http://www.theserverside.net/tt/articles/showarticle.tss?id=Top5WSMistakes</a></li>
  </ul>
  
  <p>My problem is that despite this not being recommended practice, I am stuck with having to consume a web service returning a DataSet with Java. When you generate a proxy for something like this with anything other than .NET you basically end up with an object that looks like this:</p>
  
  <pre><code>    @XmlElement(namespace = "http://www.w3.org/2001/XMLSchema", required = true)
      protected Schema schema;
      @XmlAnyElement(lax = true)
      protected Object any;
  </code></pre>
  
  <p>This first field is the actual schema that should describe the DataSet. When I process this using JAX-WS and JAXB in Java, it bring all of XS-Schema in as Java objects to be represented here. Walking the object tree of JAXB is possible but not pretty. The any field represents the raw XML for the DataSet that is in the schema specified by the schema.</p>
  
  <p>The structure of the dataset is pretty consistent but the data types do change. I need access to the type information and the schema does vary from call to call. I've though of a few options but none seem like 'good' options.</p>
  
  <ul>
  <li>Trying to generate Java objects from the schema using JAXB at runtime seems to be a bad idea. This would be way too slow since it would need to happen everytime.</li>
  <li>Brute force walk the schema tree using the JAXB objects that JAX-WS brought in.</li>
  <li>Maybe instead of using JAXB to parse the schema it would be easier to deal with it as XML and use XPath to try and find the type information I need.</li>
  </ul>
  
  <p>Are there other options I have not considered? Is there a Java library to parse DataSet objects easily? What have other people done who may have similar situations?</p>