在一个黑莓应用程序中解析一个 XML 结果,它返回表单中的节点;
<searches>
<search id ='1234'>
<name> somename </name>
<address> some address </address>
<sector> some sector </sector>
<contacts> 12345, me@me.com </contacts>
<searches>
当进行没有任何匹配的搜索时,结果返回空,即;
<name></name>
<address></address>
<sector></sector>
<contacts></contacts>
不要返回结果。它看起来就像<searches></searches>
. 在这种情况下,当搜索结果仅返回时,如何指定对话框警报<searches></searches>
这是我与解析器连接的 Http 连接。
try{
HttpConnection connection = (HttpConnection)Connector.open("http://someurl.xml",Connector.READ_WRITE);
URLEncodedPostData postData = new URLEncodedPostData(URLEncodedPostData.DEFAULT_CHARSET, false);
postData.append("username", "someusername");
postData.append("password", "somepassword");
postData.append("term", word);
connection.setRequestMethod(HttpConnection.POST);
connection.setRequestProperty("Content-Type","application/x-www-form-urlencoded");
connection.setRequestProperty("User-Agent","Profile/MIDP-2.0 Configuration/CLDC-1.0");
OutputStream requestOut = connection.openOutputStream();
requestOut.write(postData.getBytes());
connection.getHeaderField("Content-type");
DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance();
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder();
docBuilder.isValidating();
InputStream detailIn = connection.openInputStream();
doc = docBuilder.parse(detailIn);
requestOut.close();
connection.close();
NodeList list = doc.getElementsByTagName("name");
NodeList list1 = doc.getElementsByTagName("address");
NodeList list2 = doc.getElementsByTagName("sector");
NodeList list3 = doc.getElementsByTagName("contacts");
callback(list,list1,list2,list3);
requestOut.close();
connection.close();
}
catch(Exception ex){
System.out.println(ex.toString());
}
我应该使用 if 还是 for ?