Extension of Smack IQ package
If we want to extend an IQ package like this:
Element
You need to define a custom IQ to extend:
Package com.emcc.stc.demo.iq;import org.jivesoftware.smack.packet.IQ;public class SampleIQ extends IQ {String condition; String value; protected SampleIQ (String childElementName, String childElementNamespace, String condition, String value) {super (childElementName, childElementNamespace); this.condition = condition; this.value = value The most critical implementation class * / @ Override protected IQChildElementXmlStringBuilder getIQChildElementBuilder (IQChildElementXmlStringBuilder xml) {xml.attribute ("condition", condition); xml.rightAngleBracket (); xml.element ("extraElement", "value"); return xml } public static void main (String [] args) {IQ iq = new SampleIQ ("query", "emcc.jiyq", "what", "elemeng"); System.out.println (iq.toXML ());}}
The advantage of this approach is that each IQ has a specific definition, each custom IQ class has its own application scenario, and there is no need to manually configure XML.
In addition, you can write a corresponding IQParser to parse specific types of IQ packages, which is more convenient to use.
Finally, the documentation of Smack is quite missing, and it is better to read the source code carefully for a lot of problems.