2 Replies Latest reply: Sep 6, 2008 6:31 AM by 611651 RSS

    Load object from xml

    611651
      Imagine if you can that I have a XmlLoader class for each object that can be loaded from xml.
      Before I had the xmlLoader code inside the objects but it's better to separate loading from actual object...

      these xmlLoaders needs to be able to set all vars for those objects. How would be the best way to do that?
      Atm I have made all vars with a package scope. but this really opens the object open to all other objects in that package...

      I don't know any other options. so how do you keep your vars safe, but still be able to load them from whatever source(xml, txt)...?
        • 1. Re: Load object from xml
          843785
          Apache Digester is good for this sort of thing. It uses setter methods to populate your objects. You could also just use XML Serialization and deserialization. Or, you could go for something like JAXB or XMLBeans, whereby you actually will have classes generated for you from XML Schemas, and instances of those classes can marshall and unmarshall themselves quite easily
          • 2. Re: Load object from xml
            611651
            didn't knew there ware so many options avaible.
            I choose XStream
            a Note from their website: Notice that the fields are private.
            XStream doesn't care about the visibility of the fields.
            No getters or setters are needed. Also, XStream does not limit you to having a default constructor.

            sweet!, that's just what I need.