Ef Ge
Ef Ge

Reputation: 818

Parsing static class from JAXB in java

I have the following class:

@XmlRootElement 
public class MyClass{
public static MyClass myClass=new MyClass();
   @XmlElement
   public static Object variable1=0;
   @XmlElement
   public static Object variable2=0;
   @XmlElement
   public static Object variable3=0;
   public final Object constant1=5;
}

I am parsing this class from XML via JAXB:

MyClass.myClass=(MyClass )  jaxbUnmarshaller.unmarshal(reader);

But if I print out the value of one of these variables, its always 0. How can I do this? The problem is the static access. If it were a non-static class, it would work.

I want to parse this:

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>
<MyClass>
<variable1>1</variable1>
<variable2>2</variable2>
<variable3>3</variable3>
 </MyClass>

There are no exceptions or something. If I create a new class with the same variables as non-static und parse the xml as this new Class, I can access all the parsed variables.

public class Helper{ public int variable1=0; ..... }

Helper helper=(Helper) jaxbUnmarshaller.unmarshal(reader);

now I can make:

MyClass.variable1=helper.variable1;

But this is only a workaround.

Upvotes: 0

Views: 1378

Answers (1)

wumpz
wumpz

Reputation: 9201

JAXB always works for using instances of classes. If you look at

(MyClass )  jaxbUnmarshaller.unmarshal(reader);

it returns an instance of your class. It is not clear to me, why you would need your variables in a static way. You are saving your MyClass instance to a static variable myClass. At least this MyClass.myClass should hold valid values if you use it in a nonstatic way. But you have access to this instance over your static variable:

 MyClass.myClass.variable1

But if you indeed need variable1 to be a static one, then you should implement getters/setters for your class that would set your static variables. JAXB would use these instead of your variable. So you could write the values to whatever target/variable you want.

Edit 1:

So here is my piece of testcode that does set your static variables. Why would you use an Object type for variable1..3? JAXB does the typemapping for your. I changed it to int.

import java.io.StringReader;
import javax.xml.bind.JAXBContext;
import javax.xml.bind.JAXBException;
import javax.xml.bind.Unmarshaller;
import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(name = "MyClass")
public class MyClass {
    public static MyClass myClass = new MyClass();

    public static int variable1 = 0;
    public static int variable2 = 0;
    public static int variable3 = 0;

    public final Object constant1 = 5;

    @XmlElement
    public int getVariable1() {
        return variable1;
    }

    public void setVariable1(int variable1) {
        MyClass.variable1 = variable1;
    }

    @XmlElement
    public int getVariable2() {
        return variable2;
    }

    public void setVariable2(int variable2) {
        MyClass.variable2 = variable2;
    }

    @XmlElement
    public int getVariable3() {
        return variable3;
    }

    public void setVariable3(int variable3) {
        MyClass.variable3 = variable3;
    }

    public static void main(String[] args) throws JAXBException {
        String data = "<?xml version=\"1.0\" encoding=\"UTF-8\" standalone=\"yes\"?>\n"
                + "<MyClass>\n"
                + "<variable1>1</variable1>\n"
                + "<variable2>2</variable2>\n"
                + "<variable3>3</variable3>\n"
                + "</MyClass>";

        JAXBContext context = JAXBContext.newInstance(MyClass.class);
        Unmarshaller jaxbUnmarshaller = context.createUnmarshaller();

        MyClass.myClass = (MyClass) jaxbUnmarshaller.unmarshal(new StringReader(data));

        System.out.println(MyClass.variable1);
        System.out.println(MyClass.variable2);
        System.out.println(MyClass.variable3);

        System.out.println(MyClass.myClass.variable1);
        System.out.println(MyClass.myClass.variable2);
        System.out.println(MyClass.myClass.variable3);
    }
}

Upvotes: 1

Related Questions