解析从 Java 中的 .NET Web 服务返回的 .NET 数据集

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2667646/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-29 22:16:48  来源:igfitidea点击:

Parsing a .NET DataSet returned from a .NET Web Service in Java

java.netweb-servicesinteropdataset

提问by Chris Dail

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:

我必须从 Java 应用程序使用 .NET 托管的 Web 服务。两者之间的互操作性通常非常好。我遇到的问题是 .NET 应用程序开发人员选择使用 .NET DataSet 对象公开数据。关于为什么不应该这样做以及它如何使互操作性变得困难的文章有很多:

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:

我的问题是,尽管这不是推荐的做法,但我不得不使用返回带有 Java 的 DataSet 的 Web 服务。当你用 .NET 以外的任何东西为这样的东西生成一个代理时,你基本上最终会得到一个看起来像这样的对象:

    @XmlElement(namespace = "http://www.w3.org/2001/XMLSchema", required = true)
    protected Schema schema;
    @XmlAnyElement(lax = true)
    protected Object any;

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.

第一个字段是应该描述数据集的实际模式。当我在 Java 中使用 JAX-WS 和 JAXB 处理它时,它会将所有 XS-Schema 作为 Java 对象引入这里来表示。遍历 JAXB 的对象树是可能的,但并不漂亮。any 字段表示由架构指定的架构中的 DataSet 的原始 XML。

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.

数据集的结构非常一致,但数据类型确实发生了变化。我需要访问类型信息,并且架构因调用而异。我已经想到了一些选择,但似乎没有一个是“好”的选择。

  • 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.
  • Brute force walk the schema tree using the JAXB objects that JAX-WS brought in.
  • 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.
  • 尝试在运行时使用 JAXB 从模式生成 Java 对象似乎是一个坏主意。这太慢了,因为它每次都需要发生。
  • 使用 JAX-WS 引入的 JAXB 对象蛮力遍历模式树。
  • 也许不是使用 JAXB 来解析模式,而是将其作为 XML 处理并使用 XPath 尝试查找我需要的类型信息会更容易。

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?

还有其他我没有考虑过的选择吗?是否有一个 Java 库可以轻松解析 DataSet 对象?其他可能有类似情况的人做了什么?

采纳答案by Doobi

Unfortunately I don't think there's an easy answer here. What I would do is create a Proxy web service in .NET that you could call from Java that would receive the dataset, transform it into something more consumable and then return that to your Java code.

不幸的是,我认为这里没有简单的答案。我要做的是在 .NET 中创建一个代理 Web 服务,您可以从 Java 调用它来接收数据集,将其转换为更易于使用的内容,然后将其返回给您的 Java 代码。

回答by zumalifeguard

Is your Java application running on a server? If so, install Mono on the server and use ADO.NET built into Mono to convert the Dataset into something Java can understand. The advantage is that all the interpretation of the Dataset is done for you in Mono. You can even use IKVM to stay in the Java language world. Anyway, in the Mono world you can have a local service which can do the conversion, or simply have a utility that converts the Dataset into the file system in a format that Java can understand.

您的 Java 应用程序是否在服务器上运行?如果是这样,请在服务器上安装 Mono 并使用 Mono 内置的 ADO.NET 将数据集转换为 Java 可以理解的内容。优点是Dataset 的所有解释都在Mono 中为您完成。您甚至可以使用 IKVM 留在 Java 语言世界。无论如何,在 Mono 世界中,您可以拥有一个可以进行转换的本地服务,或者只是拥有一个实用程序,可以将数据集以 Java 可以理解的格式转换为文件系统。

回答by Thorbj?rn Ravn Andersen

Have you considered using XSLT on the raw XML returned to massage it into something you can handle more easily?

您是否考虑过在返回的原始 XML 上使用 XSLT 以将其转换为您可以更轻松处理的内容?

I've used that approach to convert a complex return value into something similar to

我使用这种方法将复杂的返回值转换为类似于

<ops>
  <set name="foo" value="bar"/>
  <set name="foo2" value="bar2" />
  ....
 </ops>

This moves the logic into a language well suited to process XML instead of handcoding it in Java.

这将逻辑转移到一种非常适合处理 XML 的语言中,而不是用 Java 手工编码。