[jdev] My outgoing jabber packet
Anthony Ortiz
anthonypaulo at gmail.com
Mon Mar 7 16:19:43 CST 2005
Hi Dan!
I'm pretty new on the Jabber scene, but I've been through this so I think I
can give my 2cents worth... First, what language are you working with? If
java, you can either plug your jabber input stream into the built-in crimson
parser (in the core since 1.4 I believe) like so :
// create sax parser (always practice safe sax! :P)
xr = new org.apache.crimson.parser.XMLReaderImpl();
// implement the necessary interfaces
xr.setContentHandler(this);
xr.setErrorHandler(this);
// parse the jabber input stream
xr.parse(new InputSource(JabberInputStream));
The above method will trigger your callbacks whenever it parses an element;
works great. Or you can always parse it through the DOM, but that's old hat,
don't need to regurgitate it here.
NET?? I'm in the middle of porting my jabber applet to C# so I can offer
some advice here as well. .NET currently has a bug when it comes to using
the xml reader to parse an input stream, it raised hell in the jabber
community and was quite the fuss for a while until they disovered a work
around which I presonally would not implement given that I am starting from
scratch. Instead of reading the xml straight from the jabber stream, I use
the stream BeginRead and EndRead asynchronous calls. When data arrives, I
grab the first xml fragment (by fragment I mean <tag><subtags/></tag>) via a
regex expression. I then create an XMLDocument and call the LoadXML method
to load the xml fragment in. Then I build my own custom elements array by
parsing the xml document via a recursive function. Now that all the all the
elements are in my array, I can search through them as I please.
One thing I thought up in order to save time is to set each of my custom
element objects in the resulting array to have a field called "FlatTag". The
flat tag is simply a flattened out path of the tag. For example :
Given the following XML :
<tag1>
<tag2>
<tag3>Hello<.tag3>
</tag2>
</tag1>
My resulting array would look like this :
array[0] = tag1 element, flattag = "tag1"
array[1] = tag2 element, flattag = "tag1|tag2"
array[2] = tag3 element, flattag = "tag1|tag2|tag3"
(notice I discard the end tags, no need for them)
Now all you need to do is create a function that returns your custom element
based on the flattag, like :
MyElement = GetElementByFlatTag("tag1|tag2|tag3")
This approach made my life soooooo much easier. It's so much better to call
GetElementByFlatTag("iq|si|file|range") then to have to go through nodes and
such.
Hope this helped.
cheers!
Anthony
On Mon, 07 Mar 2005 15:23:59 -0500, Dan Plesse <dplesse at optonline.net> wrote:
> While examining my outgoing data packet I see a copy of the message inside a
>
> <html xmlns="http://www.w3.org/1999/xhtml"></html> tag
>
> Example:
> <message id="m_8" type="chat" to="danp5648 at jabber.org/Exodus"><body>hello
> back</body><html xmlns="http://www.w3.org/1999/xhtml">hello
> back</html></message>
>
> I was wondering how I can use this to advance my skills beyond just text
> messages.
>
> Right now I am just peeling the text out with XML
>
> Like so:
> thisXML.firstChild.childNodes[1].childNodes[0].nodeValue
>
> Q #2 Is their an easier way parse this packet? Like using the attribute
> names and not arrays? Thanks
>
> _______________________________________________
> jdev mailing list
> jdev at jabber.org
> http://mail.jabber.org/mailman/listinfo/jdev
>
More information about the JDev
mailing list