如何使用Smack库发送自定义XML数据?

问题描述:

我正在使用Java中的Smack API连接到我的XMPP服务器.

I'm using the Smack API in Java to connect to my XMPP server.

我要发送这样的自定义消息包:

I want to send a customized message packet like this:

<message to="you@MyServer.com" type="chat" MYFIELD="custom stuff">
    <body> hi </body>
    <CUSTOM_STANZA A="..." B="..."> 
        C="..." 
        D="..."
    </CUSTOM_STANZA>
</message>

我猜我创建了自己的Packet,并以toXML()方法返回此XML.但这似乎不起作用.

I'm guessing that I create implement my own Packet with that returns this XML in it's toXML() method. But that doesn't seem to work.

任何帮助将不胜感激.

您需要定义一个自定义类,该类应实现ExtensionElement (由@ )

You need to define a custom class that should implements ExtensionElement (as menitioned by @Flow)

在此答案中可以找到产生以下节的非常详细的解释

<message id='923442621149' type='chat'><body>shanraisshan</body>
<reply xmlns='shayan:reply' rText='this is custom attribute'/>
</message>

其中回复是自定义扩展名,其中包含

where reply is a custom extension, which contains

  1. 元素(回复)
  2. 命名空间(shayan:回复)
  1. Element (reply)
  2. Namespace (shayan:reply)

默认的xmpp命名空间列表可在 XMPP官方网站

the list of default xmpp namespaces are available at Official XMPP website