views:

406

answers:

6

Hi, I've got to write a multithreaded chat program, using a server and clients but each message sent has to be in XML.

Is it simpler/easier just to write out all the code in java, and then try and somehow alter it so the messages are sent in XMl format, or would it be simpler just to try and go for it in XML and hope it works. I'll admit I don't know that much about XML. :)

Also any links to any relevant online help/tutorials would be much appreciated. Thanks.

+1  A: 

When messing with XML in Java, PLEASE consider using JAXB or something similar. It allows you to work with a normal object graph in memory and then serialize that to XML in one operation (and the other way around).

Manipulating XML through the DOM API is a slow way to lose your sanity, do not do it for any non-trivial amount of XML.

I fail to see what the program being multithreaded or a server have to do with it though...

Gerco Dries
A: 

There is something called XML RPC. This examples pretty much shows what you're looking for:

http://docstore.mik.ua/orelly/xml/jxml/ch11_02.htm

Tommy
A: 

It would be simpler to use existing XMPP clients and servers and not write your own at all.

If this is in fact homework, then I would suggest writing the client and server as you have suggested, using all java, but use a String as the message. You can then easily add parsing of the string to/from XML when all other parts are working.

Robin
A: 

I would suggest to also have a look at Betwixt and Digester. For Digester there are some tutorials which can be found in the Digister-wiki. Betwixt provides some pretty good tutorials right on its website.

Additionally to these two tools there is a list of alternatives that can be found in the Reference section of http://wiki.apache.org/commons/Digester/WhyUseDigester

zlajo
+1  A: 

Check out XStream. You can use this to marshall a normal Java object into XML, and back again into an object, without having to do anything instrusive like define interfaces or specify schema etc. i.e. it works out of the box for objects you already have defined. For most cases it's seamless in its default mode.

XStream produces a direct XML serialised representation of a Java object (i.e. XML elements represent each field of a Java object directly). You can customise this further as/when you require. If you want to define persisted objects in terms of schema (XSD) then it's not appropriate. However if you're transporting objects where persistence is short-term and you're not worried about conforming to some schema then it's definitely of use.

e.g.

            Person person = new Person("Brian Agnew");
            XStream xStream = new XStream();
            System.out.println(xStream.toXML(person));

and conversion from XML to the Person object is similarly trivial.

(note XStream is thread-safe)

Brian Agnew
A: 

You're on the right page trying to break the task into smaller pieces.

Nick