【问题标题】:Rss Parsing for Blackberry java黑莓Java的Rss解析
【发布时间】:2012-11-02 17:31:49
【问题描述】:

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

但是我的代码对单个标签工作正常,我的实际要求是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);
    }

【问题讨论】:

    标签: blackberry java-me rss


    【解决方案1】:

    您的代码片段将无法编译,因为您有两个“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);
    }
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多