我有一种情况,我很困惑如何动态解析XML。我正在使用soap web服务,我想从请求xml只获取方法,我想根据响应xml只显示结果。
场合-1
我的要求
<soapenv:Envelope
xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:ncog="http://ncogroup.com/">
<soapenv:Header/>
<soapenv:Body>
<ncog:IVR_OtsukaAbilifyRegister>
<ncog:Q_ZIP>?</ncog:Q_ZIP>
<ncog:Q_DOB>?</ncog:Q_DOB>
<ncog:Q_PIN>?</ncog:Q_PIN>
<ncog:Q_COUNTRY>?</ncog:Q_COUNTRY>
<ncog:Q_RESIDENT>?</ncog:Q_RESIDENT>
<ncog:Q_GOVT_PAID>?</ncog:Q_GOVT_PAID>
<ncog:Q_MED_ACK>?</ncog:Q_MED_ACK>
<ncog:Q_PERM_OPT_IN>?</ncog:Q_PERM_OPT_IN>
<ncog:Q_MARKETING_OPT_IN>?</ncog:Q_MARKETING_OPT_IN>
<ncog:Q_ABILIFY_60>?</ncog:Q_ABILIFY_60>
<ncog:Q_DRUG_COVERAGE>?</ncog:Q_DRUG_COVERAGE>
<ncog:Q_SOURCE>?</ncog:Q_SOURCE>
<ncog:Q_DATA_OPT_IN>?</ncog:Q_DATA_OPT_IN>
</ncog:IVR_OtsukaAbilifyRegister>
</soapenv:Body>
</soapenv:Envelope>
我的回复
<soap:Envelope
xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<soap:Body>
<IVR_OtsukaAbilifyRegisterResponse
xmlns="http://ncogroup.com/">
<IVR_OtsukaAbilifyRegisterResult>
<TriplefinResultCode>P02</TriplefinResultCode>
<Msg>Index was outside the bounds of the array.</Msg>
</IVR_OtsukaAbilifyRegisterResult>
</IVR_OtsukaAbilifyRegisterResponse>
</soap:Body>
</soap:Envelope>
在上面的示例中,我希望IVR_OtsukaAbilifyRegister
作为我的方法和我的回复Index was outside the bounds of the array.
我尝试了下面的代码,但我无法获得完美的数据
private Document parseXmlFile() {
String in = readFile();
try {
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
InputSource is = new InputSource(new StringReader(in));
return db.parse(is);
} catch (ParserConfigurationException e) {
throw new RuntimeException(e);
} catch (SAXException e) {
throw new RuntimeException(e);
} catch (IOException e) {
throw new RuntimeException(e);
}
}