2012-09-11 46 views
1

我已经开发了黑莓Java的RssParser,我成功地从Rss XML文件解析标题,但我的要求是解析从Rss imageurls。Rss分析为黑莓java

但我的代码对单个标签工作正常,我的实际要求是在drawlist方法中。如何从Rss中检索图像URL和标题标签值?

这里是我的代码:

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

       conn = (StreamConnection) Connector.open("Rss.xml"+";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("image"); 

       NodeList list = doc.getElementsByTagName("title"); 
        for (int a = 0; a < list.getLength(); a++) {  
        Node textNode1 = list.item(a).getFirstChild(); 
        listElements.addElement(textNode.getNodeValue()); 
         } 





public void drawListRow(ListField list, Graphics g, int index, int y, int w) 
    { 
     String title = (String)listElements.elementAt(index); 


     g.drawText(title, 5, 15+y, 0, w); 
    } 

回答

3

你的一小段代码片段不能编译,因为你有两个“节点列表名单”变量,还是让我们假设你确实得到了不同的列表上的图像的URL。对于需要获取图像数据的每个url,启动一个Image对象并将其存储以供以后绘制:

public void run() { 
    NodeList list = doc.getElementsByTagName("image"); 

    for (int a = 0; a < list.getLength(); a++) { 
     Node textNode1 = list.item(a).getFirstChild(); 

     imageElements.addElement(getImage(textNode.getNodeValue())); 
    } 
} 

// variation of the same method found at 
// http://stackoverflow.com/questions/4883600/how-do-i-get-to-load-image-in-j2me 
private Image getImage(String url) throws IOException { 
    HttpConnection connection = null; 
    DataInputStream inp = null; 
    int length; 
    byte[] data; 
    try { 
     connection = (HttpConnection) Connector.open(url); 
     connection.getResponseMessage(); 
     length = (int) connection.getLength(); 
     data = new byte[length]; 
     inp = new DataInputStream(connection.openInputStream()); 
     inp.readFully(data); 

     return Image.createImage(data, 0, data.length); 
    } finally { 
     if (connection != null) 
      connection.close(); 
     if (inp != null) 
      inp.close(); 

    } 
} 

public void drawListRow(Graphics g, int index, int y, int w) { 
    String title = (String) listElements.elementAt(index); 
    Image image = (Image) imageElements.elementAt(index); 

    g.drawText(title, 5, 15+y, 0, w); 
    // As I do not have experience with Blackberry I assume below method exists 
    g.drawImage(image, 5, y, 0, w); 
}