JAXB编组XMPP节 [英] JAXB marshalling XMPP stanzas

查看:108
本文介绍了JAXB编组XMPP节的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用以下代码段对邮件进行编组:

I am trying to marshall a message using the following snippet:

        JAXBContext jContext = JAXBContext.newInstance(Iq.class);
        Marshaller m = newJAXBContext.createMarshaller();
        m.setProperty(Marshaller.JAXB_FRAGMENT, Boolean.TRUE);
        m.setProperty(Marshaller.JAXB_FORMATTED_OUTPUT, true);

        Bind bind = new Bind();
        bind.setResource("resource");
        Iq iq = new Iq();
        iq.setId(iqId);
        iq.setType("set");
        iq.getAnies().add(bind);

        ByteArrayOutputStream baos = new ByteArrayOutputStream();
        m.marshal(iq, baos);

这里,Iq和Bind是由相关xmpp模式组成的对象。我的问题是,使用jaxb 2.0及更高版本,所有命名空间都在根元素中声明:

Here, Iq and Bind are the objects formed from the relevant xmpp schemas. My problem is, with jaxb 2.0 and later versions, all the namespaces are declared in the root element:

<iq from='juliet@example.com/balcony'
     id='rg1'
     type='get'  xmlns='jabber:client'  xmlns:ns1='urn:ietf:params:xml:ns:xmpp-bind'> 
    <ns1:bind>
        <ns1:resource>resource</ns1:resource>
    </ns1:bind>
</iq>

但是,这里需要的是命名空间应占据适当的位置:

But, what is needed here is that the namespaces should occupy the appropriate places:

<iq from='juliet@example.com/balcony'
     id="rg1"
     type="get" xmlns="jabber:client">
       <bind xmlns="urn:ietf:params:xml:ns:xmpp-bind">
             <resource>resource</resource>
       </bind>
</iq>

有没有办法编组xmpp节,就像你在第二个xml节中通过JAXB 2.0看到它们一样或更高版本?

Is there a way to marshall the xmpp stanzas as you see them in the 2nd xml stanza through JAXB 2.0 or later versions?

长话短说,我在这里有2个问题:
1.在适当的位置声明命名空间。
2.删除我理解的命名空间前缀可以使用NamespacePrefixMapper删除吗?但不确定,一个例子会很棒。

Long story short, I have 2 problems here: 1. Declaring the namespaces at appropriate locations. 2. removing the namespace prefix which I understand can be removed using the NamespacePrefixMapper? Not sure though, an example would be great.

推荐答案

以下情况如何?:

创建一个自定义XMLStreamWriter,它将所有命名空间声明视为默认命名空间,然后编组为:

Create a custom XMLStreamWriter that will treat all namespace declarations as default namespaces, and then marshal to that:

ByteArrayOutputStream baos = new ByteArrayOutputStream();
XMLOutputFactory xof = XMLOutputFactory.newFactory();
XMLStreamWriter xsw = xof.createXMLStreamWriter(System.out);
xsw = new MyXMLStreamWriter(xsw);
m.marshal(iq, xsw);
xsw.close();

MyXMLStreamWriter

import java.util.Iterator;

import javax.xml.namespace.NamespaceContext;
import javax.xml.stream.XMLStreamException;
import javax.xml.stream.XMLStreamWriter;

public class MyXMLStreamWriter implements XMLStreamWriter {

    private XMLStreamWriter xsw;
    private MyNamespaceContext nc = new MyNamespaceContext();

    public MyXMLStreamWriter(XMLStreamWriter xsw) throws Exception {
        this.xsw = xsw;
        xsw.setNamespaceContext(nc);
    }

    public void close() throws XMLStreamException {
        xsw.close();
    }

    public void flush() throws XMLStreamException {
        xsw.flush();
    }

    public NamespaceContext getNamespaceContext() {
        return xsw.getNamespaceContext();
    }

    public String getPrefix(String arg0) throws XMLStreamException {
        return xsw.getPrefix(arg0);
    }

    public Object getProperty(String arg0) throws IllegalArgumentException {
        return xsw.getProperty(arg0);
    }

    public void setDefaultNamespace(String arg0) throws XMLStreamException {
        xsw.setDefaultNamespace(arg0);
    }

    public void setNamespaceContext(NamespaceContext arg0) throws XMLStreamException {
    }

    public void setPrefix(String arg0, String arg1) throws XMLStreamException {
        xsw.setPrefix(arg0, arg1);
    }

    public void writeAttribute(String arg0, String arg1) throws XMLStreamException {
        xsw.writeAttribute(arg0, arg1);
    }

    public void writeAttribute(String arg0, String arg1, String arg2) throws XMLStreamException {
        xsw.writeAttribute(arg0, arg1, arg2);
    }

    public void writeAttribute(String arg0, String arg1, String arg2, String arg3) throws XMLStreamException {
        xsw.writeAttribute(arg0, arg1, arg2, arg3);
    }

    public void writeCData(String arg0) throws XMLStreamException {
        xsw.writeCData(arg0);
    }

    public void writeCharacters(String arg0) throws XMLStreamException {
        xsw.writeCharacters(arg0);
    }

    public void writeCharacters(char[] arg0, int arg1, int arg2) throws XMLStreamException {
        xsw.writeCharacters(arg0, arg1, arg2);
    }

    public void writeComment(String arg0) throws XMLStreamException {
        xsw.writeComment(arg0);
    }

    public void writeDTD(String arg0) throws XMLStreamException {
        xsw.writeDTD(arg0);
    }

    public void writeDefaultNamespace(String arg0) throws XMLStreamException {
        xsw.writeDefaultNamespace(arg0);
    }

    public void writeEmptyElement(String arg0) throws XMLStreamException {
        xsw.writeEmptyElement(arg0);
    }

    public void writeEmptyElement(String arg0, String arg1) throws XMLStreamException {
        xsw.writeEmptyElement(arg0, arg1);
    }

    public void writeEmptyElement(String arg0, String arg1, String arg2) throws XMLStreamException {
        xsw.writeEmptyElement(arg0, arg1, arg2);
    }

    public void writeEndDocument() throws XMLStreamException {
        xsw.writeEndDocument();
    }

    public void writeEndElement() throws XMLStreamException {
        xsw.writeEndElement();
    }

    public void writeEntityRef(String arg0) throws XMLStreamException {
        xsw.writeEntityRef(arg0);
    }

    public void writeNamespace(String arg0, String arg1) throws XMLStreamException {
    }

    public void writeProcessingInstruction(String arg0) throws XMLStreamException {
        xsw.writeProcessingInstruction(arg0);
    }

    public void writeProcessingInstruction(String arg0, String arg1) throws XMLStreamException {
        xsw.writeProcessingInstruction(arg0, arg1);
    }

    public void writeStartDocument() throws XMLStreamException {
        xsw.writeStartDocument();
    }

    public void writeStartDocument(String arg0) throws XMLStreamException {
        xsw.writeStartDocument(arg0);
    }

    public void writeStartDocument(String arg0, String arg1) throws XMLStreamException {
        xsw.writeStartDocument(arg0, arg1);
    }

    public void writeStartElement(String arg0) throws XMLStreamException {
        xsw.writeStartElement(arg0);
    }

    public void writeStartElement(String arg0, String arg1) throws XMLStreamException {
        xsw.writeStartElement(arg0, arg1);
    }

    public void writeStartElement(String arg0, String arg1, String arg2) throws XMLStreamException {
        xsw.writeStartElement("", arg1, arg2);
        if(null != arg2 || arg2.length() > 0) {
            String currentDefaultNS = nc.getNamespaceURI("");
            if(!arg2.equals(currentDefaultNS)) {
                writeDefaultNamespace(arg2);
                nc.setDefaultNS(arg2);
            }
        }
     }

    private static class MyNamespaceContext implements NamespaceContext {

        private String defaultNS = "";

        public void setDefaultNS(String ns) {
            defaultNS = ns;
        }

        public String getNamespaceURI(String arg0) {
            if("".equals(arg0)) {
                return defaultNS;
            }
            return null;
        }

        public String getPrefix(String arg0) {
            return "";
        }

        public Iterator getPrefixes(String arg0) {
            return null;
        }

    }
}

这篇关于JAXB编组XMPP节的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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