Convert xml to object (initializing static fields)

View: New views
2 Messages — Rating Filter:   Alert me  

Convert xml to object (initializing static fields)

by Diego Mota :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hello!

I´d like to convert XML to objects. But i want recover static final fields (or static fields only) of objects in XML files.

How can i do this?

Ex:
public class ModelObject {
   public static final String pathDB;


   public static void main(String args[]) {

        XStream xstream = new XStream(new DomDriver());
        ModelObject obj = new ModelObject();

        try {
            FileInputStream arquivo = new FileInputStream(new File("C:\\config.xml"));
            xstream.fromXML(arquivo, obj);      //will initialize the pathDB field
            arquivo.close();

        } catch (IOException e) {
            e.printStackTrace();
        }

    }

}



      ____________________________________________________________________________________
Veja quais são os assuntos do momento no Yahoo! +Buscados
http://br.maisbuscados.yahoo.com

---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email



Re: Convert xml to object (initializing static fields)

by Jörg Schaible-2 :: Rate this Message:

Reply to Author | View Threaded | Show Only this Message

Hi Diego,

Diego Mota wrote at Mittwoch, 21. Oktober 2009 00:41:

> Hello!
>
> I´d like to convert XML to objects. But i want recover static final fields
> (or static fields only) of objects in XML files.
>
> How can i do this?
>
> Ex:
> public class ModelObject {
>    public static final String pathDB;
>
>
>    public static void main(String args[]) {
>
>         XStream xstream = new XStream(new DomDriver());
>         ModelObject obj = new ModelObject();
>
>         try {
>             FileInputStream arquivo = new FileInputStream(new
>             File("C:\\config.xml"));
>             xstream.fromXML(arquivo, obj);      //will initialize the
>             pathDB field arquivo.close();
>
>         } catch (IOException e) {
>             e.printStackTrace();
>         }
>
>     }
>
> }

XStream behaves like Java serialization, it does completely ignore static
members - simply because these objects are not part of the serialized
instance at all.

However, you can write a custom converter for your type - you're free to do
what you want in your implementation - even saving and restoring static
fields. Have a look at the converter tutorial, it's easy.

- Jörg


---------------------------------------------------------------------
To unsubscribe from this list, please visit:

    http://xircles.codehaus.org/manage_email