Answer the question
In order to leave comments, you need to log in
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 zeros
<?xml version="1.0" encoding="windows-1251"?>
<product>
<preply>
<err>
<ctErr>
<Code>004</Code>
<Text>Error</Text>
</ctErr>
</err>
</preply>
</product>
@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;
}
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
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 questionAsk a Question
731 491 924 answers to any question