apache digester sax解析器异常

时间:2016-04-13 02:19:07

标签: apache-commons-digester

我正在尝试使用Apache Digester来解析我的SOAP响应,但是我得到了SaxParserException。这就是我所拥有的:

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 + "]";
    }

}

但是这段代码给了我SaxParserException。我猜我没有给出正确的模式。有人可以建议吗?

1 个答案:

答案 0 :(得分:0)

您需要超越SaxParserException到根本原因。这是NullPointerException方法中的addTaskResponses。从那里开始,很明显taskResponses从未初始化,因此您需要在尝试向其添加内容之前创建一个列表:

private List<TaskResponse> taskResponses = new ArrayList<>();

干杯