2016-04-13 69 views
0

我試圖使用Apache Digester來解析我的SOAP響應,但得到了SaxParserException。以下是我有:apache digester sax解析器異常

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。我猜測我沒有給出正確的模式。有人可以建議嗎?

回答

0

您需要超越SaxParserException以查看根本原因。這是addTaskResponses方法中的NullPointerException。從那裏,很明顯的是,taskResponses從未初始化,所以你需要把東西添加到它之前創建一個列表:

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

乾杯

+0

感謝@Barney。這解決了'SaxParserException'。我在這裏很愚蠢。你能告訴我應該是什麼樣的模式,以便得到'a:WorkOrderID'的價值嗎? – Praz

+0

@Praz:從來沒有使用過註釋,但我會嘗試給它的完整路徑而不是相對的 - 即:Envelope/s:Body /.../a:Task/a:WorkOrderID'。還要注意,在你的代碼中,你沒有'setWorkorderId'方法(看起來像剪切和粘貼錯誤 - 它被稱爲'setTaskResponseList'),因此'BeanPropertySetter'找不到匹配項 - 這可能也是你的問題。 – Barney

+0

好的。設置方法和相對路徑是問題。非常感謝。 – Praz