0
所以我已經從這裏獲得了幫助,所以我想了解爲什麼不再試一次!?任何建議將不勝感激。從Java中的HttpResponse XML中提取屬性值
我正在使用HTTP客戶端併發出POST請求;響應是一個XML的身體,看起來像下面這樣:
<?xml version="1.0" encoding="UTF-8"?>
<invokeResult>
<method name="RS">
<params>
<param name="sp" value="" />
<param name="cp" value="" />
<param name="ck" value="" />
</params>
</method>
<status>OK</status>
<result>
<status>ENDED</status>
<reportUrl></reportUrl>
<runId></runId>
<pass count="0" />
<fail count="1" />
<message>column,report</message>
</result>
</invokeResult>
現在...
我有一個HttpEntity這是
String responseString = EntityUtils.toString(response.getEntity());
我想獲得的<pass count="0" />
從價值通過java代碼的xml響應。有人可以幫我嗎?
我做的詳細代碼,但得到空指針異常。
DefaultHttpClient client = new DefaultHttpClient();
HttpGet request = new HttpGet("REQUEST-URL");
HttpResponse response = client.execute(request);
if (response.getStatusLine().getStatusCode() == 200 && response.getEntity() != null) {
String responseString = EntityUtils.toString(response.getEntity());
responseString=responseString.replaceAll("<?xml*?>", "").trim();
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory
.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
Document doc = docBuilder.parse(new InputSource(new StringReader(
responseString)));
NodeList orderNode = doc.getElementsByTagName("<pass count=");
String strOrdNo = orderNode.item(0).getTextContent();
logger.info("pass Value = " + strOrdNo);
感謝
在哪一行中可以獲得NPE? –
正在獲得NPE NodeList orderNode = doc.getElementsByTagName(「
user3175778
你只需要在那裏傳遞標籤名稱; 'doc.getElementsByTagName( 「通」)'。請檢查我的答案。 –