2011-10-06 108 views
3

我能夠使用kso​​ap2讓我複雜的階級響應,現在我正在repsonse是XML形式KSOAP解析查詢

任何人都可以幫我如何解析這個KSOAP響應??我已閱讀論壇爲它添加映射我也添加了映射。我可以使用XML解析技術來解析這個。請給我一個合適的方法。 感謝

+0

其實你需要做什麼?只是爲了解析XML值?或者需要將值傳遞給webservice? –

+1

請參閱此鏈接我已經發布了xml,我想解析。我需要解析xml http://supportforums.blackberry.com/t5/Java-Development/Ksoap-Parsing-Query/td-p/ 1346685 –

回答

0

使用這個XML解析代碼和解析:

public class xmlparsing extends UiApplication implements ListFieldCallback, FieldChangeListener 
{ 

public static void main(String[] args) 
{ 
xmlparsing app = new xmlparsing(); 
app.enterEventDispatcher(); 
} 


public long mycolor ; 
Connection _connectionthread; 
private static ListField _list; 
private static Vector listElements = new Vector(); 
public MainScreen screen = new MainScreen(); 
VerticalFieldManager mainManager; 
VerticalFieldManager subManager; 



public xmlparsing() 
{ 
super(); 
pushScreen(screen); 

final Bitmap backgroundBitmap = Bitmap.getBitmapResource("blackbackground.png"); 

mainManager = new VerticalFieldManager(Manager.NO_VERTICAL_SCROLL ,Manager.NO_VERTICAL_SCROLLBAR) 
{ 

public void paint(Graphics graphics) 
{ 
graphics.drawBitmap(0, 0, Display.getWidth(),Display.getHeight(),backgroundBitmap, 0, 0); 

super.paint(graphics); 
} 

}; 

subManager = new VerticalFieldManager(Manager.VERTICAL_SCROLL | Manager.VERTICAL_SCROLLBAR) 
{ 
protected void sublayout(int maxWidth, int maxHeight) 
{ 
int displayWidth = Display.getWidth(); 
int displayHeight = Display.getHeight(); 

super.sublayout(displayWidth, displayHeight); 
setExtent(displayWidth, displayHeight); 
} 
}; 


screen.add(mainManager); 

_list = new ListField() 

{ 

public void paint(Graphics graphics) 

{ 
graphics.setColor((int) mycolor); 
super.paint(graphics); 

} 

}; 
mycolor = 0x00FFFFFF; 
_list.invalidate(); 
_list.setEmptyString("* Feeds Not Available *", DrawStyle.HCENTER); 
_list.setRowHeight(50); 
_list.setCallback(this); 
mainManager.add(subManager); 
listElements.removeAllElements(); 
_connectionthread = new Connection(); 
_connectionthread.start(); 
} 


protected boolean navigationClick(int status, int time) 
{ 
try 
{ 
//navigate here to another screen if you need. 
} 
catch(Exception e) 
{ 
System.out.println("Exception:- : navigationClick() "+e.toString()); 
} 
return true; 
} 



private class Connection extends Thread 
{ 
public Connection() 
{ 
super(); 
} 

public void run() { 
Document doc; 
StreamConnection conn = null; 
InputStream is = null; 
try { 

conn = (StreamConnection)  Connector.open("http://timesofindia.feedsportal.com/c/33039/f/533917/index.rss"+";deviceside=true"); 

DocumentBuilderFactory docBuilderFactory = DocumentBuilderFactory.newInstance(); 
docBuilderFactory.setIgnoringElementContentWhitespace(true); 
docBuilderFactory.setCoalescing(true); 
DocumentBuilder docBuilder = docBuilderFactory.newDocumentBuilder(); 
docBuilder.isValidating(); 
is = conn.openInputStream(); 
doc = docBuilder.parse(is); 
doc.getDocumentElement().normalize(); 
NodeList list = doc.getElementsByTagName("title"); 
for (int i = 0; i < list.getLength(); i++) { 
Node textNode = list.item(i).getFirstChild(); 
listElements.addElement(textNode.getNodeValue()); 
} 

} catch (Exception e) { 
System.out.println(e.toString()); 
} finally { 
if (is != null) { 
try { is.close(); 
} catch (IOException ignored) {} 
} if (conn != null) { 
try { conn.close(); } 
catch (IOException ignored) {} 
} } UiApplication.getUiApplication().invokeLater(new Runnable() { 
public void run() { 
_list.setSize(listElements.size()); 
subManager.add(_list); 
screen.invalidate(); 
} 
}); 
} 

} 


public void drawListRow(ListField list, Graphics g, int index, int y, int w) 
{ 
String tes = (String)listElements.elementAt(index); 
int yPos = 0+y; 
g.drawLine(0, yPos, w, yPos); 
g.drawText(tes, 5, 15+y, 0, w); 
} 


public Object get(ListField list, int index) 
{ 
return listElements.elementAt(index); 
} 
public int indexOfList(ListField list, String prefix, int string) 
{ 
return listElements.indexOf(prefix, string); 
} 
public int getPreferredWidth(ListField list) 
{ 
return Display.getWidth(); 
} 
public void insert(String toInsert, int index) { 
listElements.addElement(toInsert); 
} 

public void fieldChanged(Field field, int context) { 


} 
}