I'm trying to use Apache Digester to parse my SOAP response, but am getting SaxParserException. Here is what I have:
Main.java
Digester digester = org.apache.commons.digester3.binder.DigesterLoader
.newLoader(new FromAnnotationsRuleModule() {
@Override
protected void configureRules() {
bindRulesFrom(SubmitResponse.class);
}
}).newDigester();
SubmitResponse response = digester.parse(new ByteArrayInputStream(
"<s:Envelope xmlns:s=\"http://schemas.xmlsoap.org/soap/envelope/\"><s:Body><SubmitResponse xmlns=\"http://schemas.example.com/digitalrequest/v5.00\"><SubmitResult xmlns:a=\"http://schemas.datacontract.org/2004/07/example.Lib.v500\" xmlns:i=\"http://www.w3.org/2001/XMLSchema-instance\"><a:ID>0</a:ID><a:TaskResponses><a:TaskResponse><a:Task><a:WorkOrderID>0</a:WorkOrderID></a:Task></a:TaskResponse></a:TaskResponses></SubmitResult></SubmitResponse></s:Body></s:Envelope>"
.getBytes()));
SubmitResponse.java
@ObjectCreate(pattern = "s:Envelope")
public class SubmitResponse implements Serializable {
private static final long serialVersionUID = 1L;
private List<TaskResponse> taskResponses;
public List<TaskResponse> getTaskResponses() {
return taskResponses;
}
public void setTaskResponses(List<TaskResponse> taskResponses) {
this.taskResponses = taskResponses;
}
@SetNext
public void addTaskResponses(TaskResponse taskResponse) {
this.taskResponses.add(taskResponse);
}
}
TaskResponse.java
@ObjectCreate(pattern = "s:Envelope/s:Body/SubmitResponse/SubmitResult/a:TaskResponses/a:TaskResponse")
public class TaskResponse implements Serializable {
private static final long serialVersionUID = 1L;
@BeanPropertySetter(pattern = "a:Task/a:WorkOrderID")
private String workorderId;
public String getWorkorderId() {
return workorderId;
}
public void setTaskResponseList(String workorderId) {
this.workorderId = workorderId;
}
@Override
public String toString() {
return "TaskResponse [workorderId=" + workorderId + "]";
}
}
But this code is giving me SaxParserException. I'm guessing that I'm not giving the correct pattern. Can somebody please suggest?
You need to look beyond the
SaxParserExceptionto the root cause. This is aNullPointerExceptionin theaddTaskResponsesmethod. From there, it is evident that thetaskResponsesis never initialised, so you need to create a list before trying to add things to it:Cheers