如果命名空间声明在 SOAP 信封上,如何使用 JAXB 解组 SOAP 响应? [英] How to unmarshall SOAP response using JAXB if namespace declaration is on SOAP envelope?

查看:31
本文介绍了如果命名空间声明在 SOAP 信封上,如何使用 JAXB 解组 SOAP 响应?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

JAXB 只能在已删除soap 信封的情况下解组XML.但是,我试图解组的 SOAP 响应在soap 信封上有其名称空间声明.如果我删除了soap 信封,命名空间声明也将被删除.因此,标签的前缀将指代无.这会导致 JAXB 抛出错误.如果命名空间是在 SOAP 信封上声明的,我如何使用 JAXB 解组 SOAP 响应?

JAXB can only unmarshall an XML if the soap envelope has already been removed. However, the SOAP response I'm trying to unmarshall has its namespace declaration on the soap envelope. If I remove the soap envelope, the namespace declaration will also be removed. As such the prefix of the tags will be referring to none. This causes JAXB to throw an error. How can I unmarshall a SOAP response using JAXB if the namespace is declared on the SOAP envelope?

我需要解组的类似 XML 示例如下:

A similar sample of the XML that I need to unmarshall is below:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:ns="http://example.com/">
    <soapenv:Body>
        <ns:getNumberResponse>
            <number>123456789</number>
        </ns:getNumberResponse>
    </soapenv:Body>
</soapenv:Envelope>

如果我取下肥皂信封会发生以下情况:

This is what happens if I remove the soap envelope:

<ns:getNumberResponse>
    <number>123456789</number>
</ns:getNumberResponse>

如果我删除了soap 信封,命名空间声明也将消失.JAXB 将无法解组上述 xml,因为缺少前缀ns"的命名空间定义.因此,它将返回一个 未绑定元素ns:getNumberResponse"的前缀ns"." 错误消息.我该如何解决?

If I removed the soap envelope, the namespace declaration will also be gone. JAXB won't be able to unmarshall the above xml because the namespace definition for the prefix "ns" is missing. As such it will return a "The prefix "ns" for element "ns:getNumberResponse" is not bound." error message. How do I fix it?

请注意,我使用的是 JDK 1.5.我不确定在 JDK 1.6 中是否有办法解决这个问题.我能够使用 JAXB,因为我下载了使用 JAXB 所需的 jar.

Please note that I'm using JDK 1.5. I'm not sure if there is a way to solve this in JDK 1.6. I was able to use JAXB because I downloaded the required jars for using JAXB.

推荐答案

您可以使用 StAX 解析器来解析 SOAP 消息,然后将 XMLStreamReader 推进到 getNumberResponseelement 并使用采用类参数的 unmarshal 方法.Java SE 6 中包含 StAX 解析器,但您需要为 Java SE 5 下载一个.Woodstox 是一种流行的 StAX 解析器.

You can use a StAX parser to parse the SOAP message and then advance the XMLStreamReader to the getNumberResponse element and use the unmarshal method that takes a class parameter. A StAX parser is included in Java SE 6, but you will need to download one for Java SE 5. Woodstox is a popular StAX parser.

回复

package forum11465653;

public class Response {

    private long number;

    public long getNumber() {
        return number;
    }

    public void setNumber(long number) {
        this.number = number;
    }

}

演示

XMLStreamReader 有一个 NamespaceContext.NamespaceContext 知道当前级别的所有活动命名空间声明.您的 JAXB (JSR-222) 实现将能够利用它来获取必要的信息.

An XMLStreamReader has a NamespaceContext. The NamespaceContext knows all the active namespace declarations for the current level. Your JAXB (JSR-222) implementation will be able to leverage this to get the necessary information.

package forum11465653;

import java.io.FileReader;
import javax.xml.bind.*;
import javax.xml.stream.*;

public class Demo {

    public static void main(String[] args) throws Exception{
        XMLInputFactory xif = XMLInputFactory.newFactory();
        XMLStreamReader xsr = xif.createXMLStreamReader(new FileReader("src/forum11465653/input.xml"));
        xsr.nextTag(); // Advance to Envelope tag
        xsr.nextTag(); // Advance to Body tag
        xsr.nextTag(); // Advance to getNumberResponse tag
        System.out.println(xsr.getNamespaceContext().getNamespaceURI("ns"));

        JAXBContext jc = JAXBContext.newInstance(Response.class);
        Unmarshaller unmarshaller = jc.createUnmarshaller();
        JAXBElement<Response> je = unmarshaller.unmarshal(xsr, Response.class);
        System.out.println(je.getName());
        System.out.println(je.getValue());
    }

}

输出

http://example.com/
{http://example.com/}getNumberResponse
forum11465653.Response@781f6226

这篇关于如果命名空间声明在 SOAP 信封上,如何使用 JAXB 解组 SOAP 响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆