忽略Apache Axis 1.4中的意外元素的任何解决方法? [英] Any workaround for ignoring unexpected elements in Apache Axis 1.4?
问题描述
在Apache AXIS忽略/跳过其他元素之前询问了此问题在2012年为Apache Axis 2解析时,Axis 1.4还没有解决方法吗?
The problem was asked before "Apache AXIS Ignore/Skip additional element while parsing" in 2012 for Apache Axis 2. Is there no workaround yet for Axis 1.4?
问题定义
例如;
1 - 我们有一个soap响应定义('ResponseGetCustomerInfo')在我们的wsdl开发时[使用 Axis 1.4 ]:
1- We have a soap response definition('ResponseGetCustomerInfo') in our wsdl while development[with Axis 1.4]:
...
<xs:element name="ResponseGetCustomerInfo">
<xs:complexType>
<xs:sequence>
<xs:element ref="ns1:CustomerID"/>
<xs:element ref="ns1:CustomerUsername"/>
</xs:sequence>
</xs:complexType>
</xs:element>
<xs:element name="CustomerID" type="xs:integer"/>
<xs:element name="CustomerUsername" type="xs:string"/>
...
2 - 很高兴见到这一点当我们得到这样的结果时,响应是可解析的:
2- Is good to see that response is parsable when we get like this:
<?xml version="1.0" encoding="utf-8" ?>
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<ResponseGetCustomerInfo xmlns="http://tempUri.org/">
<CustomerID>1</CustomerID>
<CustomerUsername>raki</CustomerUsername>
</ResponseGetCustomerInfo>
</soap:Body>
</soap:Envelope>
3 - 一段时间后,我们的服务提供商更改了服务响应,为响应添加新的输出字段,我们不知道何时或为何;
3- After some time, our service provider changed the service response and adds new output fields to response and we don't know when or why;
<?xml version="1.0" encoding="utf-8" ?>
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<ResponseGetCustomerInfo xmlns="http://tempUri.org/">
<CustomerID>1</CustomerID>
<CustomerUsername>raki</CustomerUsername>
<CustomerName>Raki</CustomerName>
<CustomerSurname>Bandao</CustomerSurname>
</ResponseGetCustomerInfo>
</soap:Body>
</soap:Envelope>
4 - 新响应理论上与旧版本兼容,因为没有删除或更改的字段。但是Axis无法解析响应:
4- New response theoretically compatible with older version because of no field neither deleted nor changed. But Axis can not parse the response:
"SAXException: Invalid Element ... "
我不想更新wsdl并再次重新生成Web服务客户端 。那么,有没有办法在响应中跳过意外的[新添加的]元素?或者任何解决方法?
I don't want to update wsdl and regenerate web service client again. So, Is there any way to skip "Unexpected[newly added] elements" in the response? or any workaround?
我正在尝试很多方法,但还找不到任何解决方案。
I am trying many ways, but could not find any solution yet.
推荐答案
由于供应商写这些服务,我们总是经历这个地狱。
We always go through this hell due to bad vendors writing these services.
所以,不幸的是,没有办法使用 WSDL2JAVA ,但有一个解决方法,你至少会重新生成一次存根:
So, unfortunately, there's no way out using parameters for WSDL2JAVA, BUT there is a workaround, you'll to re-generate stubs at least once:
- 替换
xs:sequence
withxs:all
。这允许以任何顺序返回元素,并且有助于修复很多情况,以及生成的存根代码,这使得步骤更容易 - 对不起,但是对于每个响应bean,你从生成的代码(例如ResponseGetCustomerInfo.java)进入它的类,而不是这个:
- Replace
xs:sequence
withxs:all
. This allows elements to be returned in any order, and helps fix a lot of cases, as well as generated stub code which makes it easier for step - Sorry, but you for each response bean, you go into its class from the generated code (such as ResponseGetCustomerInfo.java), and instead of this:
while(!reader.isStartElement() && !reader.isEndElement())
reader.next();
if(reader.isStartElement())
// A start element we are not expecting indicates a trailing invalid
// property
throw new org.apache.axis2.databinding.ADBException("Unexpected subelement " + reader.getLocalName());
有这个:
while(!reader.isStartElement() && !reader.isEndElement())
reader.next();
// if(reader.isStartElement())
// A start element we are not expecting indicates a trailing invalid
// property
// The below is commented, to prevent unexpected result parameters from causing an exception
// (As well as the condition above is removed)
// throw new org.apache.axis2.databinding.ADBException("Unexpected subelement " + reader.getLocalName());
它已经过测试,至少比没有解决方案更好。
It's tested and works at least better than no solution.
这篇关于忽略Apache Axis 1.4中的意外元素的任何解决方法?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!