Java 读取Soap响应中的XML时命名空间错误(非空)

Java 读取Soap响应中的XML时命名空间错误(非空),java,xml,soap,jaxb,xml-namespaces,Java,Xml,Soap,Jaxb,Xml Namespaces,我对SOAP服务有一个问题,它返回直接嵌入在SOAP XML中的XML文档。SOAP响应如下所示: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"> <soap:Header /> <soap:Body xmlns="WebserviceNamespace"> <Result xmlns=&q

我对SOAP服务有一个问题,它返回直接嵌入在SOAP XML中的XML文档。SOAP响应如下所示:

<soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/">
    <soap:Header />
    <soap:Body xmlns="WebserviceNamespace">
        <Result xmlns="WebserviceNamespace">
            <ActualXmlDocument DtdRelease="0" DtdVersion="4" xmlns="">
                ...
            </ActualXmlDocument>
        </Result>
    </soap:Body>
</soap:Envelope>
我得到以下例外

UnmarshalException:
    unexpected element (URI:"WebserviceNamespace", local:"ActualXmlDocument").
    Expected elements are <{}ActualXmlDocument>
解组异常:
意外元素(URI:“WebserviceNamespace”,本地:“ActualXmlDocument”)。
预期的要素是
因此,由于某些原因,在读取XML文档时,空名称空间不会被视为新的默认名称空间,而是被放错位置的WebseriveNamespace覆盖

那么我如何解决这个问题呢?我不想仅仅为了适应这种明显错误的行为而触碰XSD生成的文件。此外,我无法控制Web服务的服务器端,因此无法更改其行为。我现在看到的唯一可能性是

是否有其他方法获得具有正确命名空间的节点

编辑:这实际上是中的一个bug,在中得到了解决,但不幸的是在JAX WS本身中没有解决。

受我的启发,我实现了一个解决方案,这不是最好的方法,因为它需要将DOM序列化为XML文档:

JAXBContext context = JAXBContext.newInstance(ActualXmlDocument.class);
Unmarshaller unmarshaller = context.createUnmarshaller();

SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
saxParserFactory.setNamespaceAware(false);
XMLReader xmlReader = saxParserFactory.newSAXParser().getXMLReader();

Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "no");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
OutputStreamout = new ByteArrayOutputStream();
StreamResult streamResult = new StreamResult(out);
transformer.transform(new DOMSource(result), streamResult);

InputStream in = new ByteArrayInputStream(out.toByteArray())
SAXSource source = new SAXSource(xmlReader, new InputSource(in));

unmarshaller.unmarshal(source);
UnmarshalException:
    unexpected element (URI:"WebserviceNamespace", local:"ActualXmlDocument").
    Expected elements are <{}ActualXmlDocument>
JAXBContext context = JAXBContext.newInstance(ActualXmlDocument.class);
Unmarshaller unmarshaller = context.createUnmarshaller();

SAXParserFactory saxParserFactory = SAXParserFactory.newInstance();
saxParserFactory.setNamespaceAware(false);
XMLReader xmlReader = saxParserFactory.newSAXParser().getXMLReader();

Transformer transformer = TransformerFactory.newInstance().newTransformer();
transformer.setOutputProperty(OutputKeys.INDENT, "no");
transformer.setOutputProperty(OutputKeys.METHOD, "xml");
OutputStreamout = new ByteArrayOutputStream();
StreamResult streamResult = new StreamResult(out);
transformer.transform(new DOMSource(result), streamResult);

InputStream in = new ByteArrayInputStream(out.toByteArray())
SAXSource source = new SAXSource(xmlReader, new InputSource(in));

unmarshaller.unmarshal(source);