1

我已经为 Blackberry java 开发了 RssParser,并且我成功地从 Rss xml 文件中解析了标题,但我的要求是也解析来自 Rss 的 imageurls。

但是我的代码对单个标签运行良好,我的实际要求在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);
    }
4

1 回答 1

3

您的代码片段将无法编译,因为您有两个“NodeList 列表”变量,但假设您确实在不同的列表中获得了图像 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);
}
于 2012-09-11T11:34:46.063 回答