Discussions

XML & Web services: XML to runtime java objects- dynamic conversion?

  1. XML to runtime java objects- dynamic conversion? (8 messages)

    hi,
       I am in need to convert the XML file in to appropriate java runtime objects..
    For example:
    <Information>
    <Person>
      <Name>XXX</Name>
      >Number>243</Number>
    </Person>
    <Address>
      <Add1>Church street</Add1>
      <Add2>Church street</Add2>
    </Address>
    </Information>

    If I give this input file as input, the Objects of type Person and Address is desired to be filled with data and come out as output. Is there any way to do it?? are there libraries already available???

    Would be grateful if you guys reply to this message.
    regards,
    ramnath.
  2. Why not DOM?
  3. Why not SAX?

    No, seriously, I'd suggest you lookup some SAX parsing tutorial and code a handler class, which populates the whatever-objects. There's probably sample code which is very close to what you need...
  4. Good to hear from you. Thank you very much Gal and Vivek. Both the libraries suggested by you was really useful to me. The class

    JOXBeanBuilder class from the project (http://www.wutka.com/jox.html) exactly does what I want :).

    and ofcourse the method
    Object readObject(Class obClass)
    forms the dynamic instance of the class template input, using the XML file supplied. Excellent and powerful.

    I have one more small issue... This JOXBeanBuilder fills the value in to the bean using its getter and setter method at runtime. Out of curisoity, I want to know, if such kind of builder is available for normal classes also (my current classes are in these forms :( ..possibly you can treat those classes as a kind of structures in C which does nothing except to hold the data).

    Thanks again.
  5. Actually, the runtime representation of an XML document (or fragment) should probably not be DOM.
    If you plan to use the object more than once, a DOM instance requires that its handlers burrow down the node tree searching for Strings in each case. If the object is to be used extensivley this is a maintainence nightmare (someone changes the name of an attribute and _you_ have to chase down all the occurences of that String).

    A better approach in my opinion is to maintain a tight object binding as the xml comes in the door. The problem with this is its lack of generality - every type of document, or fragment of document, that you will be able to handle must be determined in advance.

    So what I would do is write a generic data-bean factory to convert an XML heirarchy into a data-bean. Whether you use containment (one bean per element) or some more efficient optimisation, you can then use the bean without the code-rot that passing DOMs throught system engenders.

    Alternatively, if you are writing over the wire from java to java, you could prepare data-beans to send, then use the standard java.beans.XMLEncoder and java.beans.XMLDecoder to write and read the beans to and from XML.
  6. castor!!!!![ Go to top ]

    thats right, best for data binding. castor.org
  7. See http://jxv.sourceforge.net (an open-source project of mine, might be relevant).

    Gal
  8. Check this out this might be helpful to u .
    http://www.wutka.com/jox.html

    This does excatly the same u want to achieve. this site references some other work on the same line.

    hope this helps.

    Regards
    Vivek
  9. Hi,
    You can use Castor. This is very commonly used for java object and xml binding, and the best part is its an opensource.
    Cheers