1

我想从 xml 文件中获取 BlackBerry 应用程序中的数据。我研究发现使用 kxml2 是最好的选择。但我在黑莓上找不到它的示例代码。我打开 kxml2.jar,我看到 xmlParser 和 kxmlParser,我应该使用哪一个?

有人能帮我解释一下如何清楚地使用 kxml2 吗?

4

1 回答 1

1

一个普通的 XML 解析和在 listfield 示例中显示在这里.. 基本不要太复杂..

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) {


}
}

完整项目在这里。

于 2011-11-25T11:50:19.113 回答