javax.xml.bind.JAXBContext string xml to object



I'm trying to cast an xml to an object and print idObject but always getting null value.


text.xml



<startProcessInstanceResponse xmlns="urn:wf">
<idObject>ff8081814b34a0fc014b8975ef6b10c0</idObject>
<idProcess>005486</idProcess>
</startProcessInstanceResponse>


StartProcessInstanceResponse.class



import javax.xml.bind.annotation.XmlElement;
import javax.xml.bind.annotation.XmlRootElement;

@XmlRootElement(namespace="urn:wf")
public class StartProcessInstanceResponse {

private String idObject;
private String idProcess;

public String getIdObject() {
return idObject;
}

@XmlElement
public void setIdObject(String idObject) {
this.idObject = idObject;
}

public String getIdProcess() {
return idProcess;
}

@XmlElement
public void setIdProcess(String idProcess) {
this.idProcess = idProcess;
}

}


my test function



StartProcessInstanceResponse resp =null;
try {
String respStr = "";
BufferedReader br = new BufferedReader(new FileReader("C:\\Users\\ismail\\Desktop\\test.xml"));

String sCurrentLine;
while ((sCurrentLine = br.readLine()) != null) {
respStr+=sCurrentLine;
}

JAXBContext jaxbContext = JAXBContext.newInstance(StartProcessInstanceResponse.class);
Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();
resp = (StartProcessInstanceResponse) jaxbUnmarshaller.unmarshal(new ByteArrayInputStream(respStr.getBytes()));

//the output is null
System.out.print(resp.getIdProcess());
} catch (Exception e) {
e.printStackTrace();
}

No comments:

Post a Comment