kxml2 在黑莓上解析 xml 文件

发布于 2024-12-18 04:02:10 字数 162 浏览 0 评论 0原文

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

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

I want to get data in my BlackBerry app from an xml file. I reseach and see that using kxml2 is the best choice.
But I can not find example code for it on Blackberry. I open kxml2.jar, I see xmlParser and kxmlParser, which one should I use?

Can anbody help me expalin how to use kxml2 clearly?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

秋心╮凉 2024-12-25 04:02:10

正常的 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) {


}
}

完整项目在这里。

A normal XML parsing and displaying in listfield sample is here.. don't go for too complex this at basic..

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


}
}

Full Project is here.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文