开发者

How can I convert a JAX-WS header into a String?

I need to get a value out of a SOAP message's header and I'm using JAX-WS. Grabbing data out of the header is not easy, here's what I've got so far:

    @Resource
    private WebServiceContext context;
...

    HeaderList headerList = (HeaderList) context.开发者_如何学编程getMessageContext().get(JAXWSProperties.INBOUND_HEADER_LIST_PROPERTY);
    Header header = headerList.get(0);

I want to turn this header into its xml representation, but the Header API does not look easy. I think I'm supposed to say header.readHeader(); That returns an XMLStreamReader (which is neither a Stream nor a Reader) and from there it's like working with an interface similar to an Iterator or an Enumeration or the DOM api.

What's the easiest way I can convert this header into its xml representation?


    public static String prettyPrintXML(XMLStreamReader xmlStreamReader) {
    ByteArrayOutputStream baos = new ByteArrayOutputStream();

    try {
        Transformer serializer = TransformerFactory.newInstance().newTransformer();
        serializer.setOutputProperty(OutputKeys.INDENT, "yes");
        serializer.setOutputProperty(OutputKeys.OMIT_XML_DECLARATION, "yes");
        serializer.setOutputProperty("{http://xml.apache.org/xslt}indent-amount", "2");
        serializer.transform(new StAXSource(xmlStreamReader), new StreamResult(baos));
    } catch (Exception e) {
        throw new RuntimeException(e);
    }

    String result = baos.toString();
    try {
        baos.close();
    } catch (IOException e) {
        throw new RuntimeException(e);
    }
    return result;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜