P
P
power_ranger2021-10-05 11:40:08
Java
power_ranger, 2021-10-05 11:40:08

JAVA..JAXB. Problem with unmarshaling child tags. How to fix?

I'm recently learning JAXB, I need to unmarshal the contents of Code AND Text tags. It doesn't work for me, because when outputting only zeros615c0e791658b045954684.png

<?xml version="1.0" encoding="windows-1251"?>
<product>
    <preply>
        <err>
            <ctErr>
                <Code>004</Code>
                <Text>Error</Text>
            </ctErr>
        </err>
    </preply>
</product>

Here is my Product class:

@XmlRootElement
public class Product {
    private int Code;
    private String Text;

    public int getCode() {
        return Code;
    }
    @XmlElement(name="Code")
    public void setCode(int code) {
        Code = code;
    }

    public String getText() {
        return Text;
    }
    @XmlElement(name = "Text")
    public void setText(String text) {
        Text = text;
    }


And the calling class code:
public class Main {

    public static void main(String[] args) throws JAXBException {

        try{
            File file = new File("error.xml");
            JAXBContext context = JAXBContext.newInstance(Product.class);
            Unmarshaller unmarshaller = context.createUnmarshaller();
            Product error = (Product) unmarshaller.unmarshal(file);
            System.out.println(error.getCode());
            System.out.println(error.getText());
        } catch (JAXBException e) {
            e.printStackTrace();
        }
    }
}

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Dmitry Roo, 2021-10-05
@xez

The model is obviously wrong.
It should be something like this:

public class Product {
   Preply  preply;
}

public class Preply {
   Err err;
}

public class Err {
   CtErr ctErr;
}

public class CtErr {
  int Code;
  String Text;
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question