0

我是黑莓开发应用程序的新手。我尝试将所有 xml 解析数据存储到一个对象,并将它们设置为一个向量。

public class XmlParser extends MainScreen {
    Database d;
    private HttpConnection hcon = null;

    private Vector binN;
    public Vector getBinN() {
        return binN;
    }

    public void setBinN(Vector bin) {
        this.binN = bin;
    }

    LabelField from;
    LabelField ttl;
    LabelField desc;
    LabelField date;

    public XmlParser() {
        LabelField title = new LabelField("Headline News" ,LabelField.HCENTER|LabelField.USE_ALL_WIDTH);
        setTitle(title);

        try {
            URI myURI = URI.create("file:///SDCard/Database/WebFeed.db"); 
            d = DatabaseFactory.open(myURI);
            Statement st = d.createStatement("SELECT feed_url, feed_name FROM WebFeed");
            st.prepare();
            Cursor c = st.getCursor();
            while (c.next()) {
                Row r = c.getRow();
                hcon = (HttpConnection)Connector.open(r.getString(0));
                hcon.setRequestMethod(HttpConnection.GET);
                        hcon.setRequestProperty("User-Agent", "Profile/MIDP-1.0 Configuration/CLDC-1.0");
                        hcon.setRequestProperty("Content-Length", "0");
                        hcon.setRequestProperty("Connection", "close");
                DocumentBuilderFactory factory = DocumentBuilderFactory.newInstance();
                DocumentBuilder builder = factory.newDocumentBuilder();

                builder.isValidating();
                Document document = builder.parse(hcon.openInputStream());

                Element rootElement = document.getDocumentElement();
                rootElement.normalize();

                NodeList list = document.getElementsByTagName("item");

                int i=0;
                while (i<10){
                    Node item = list.item(i);
                    if(item.getNodeType() != Node.TEXT_NODE) {
                        NodeList itemChilds = item.getChildNodes();
                        int j=0;
                        while (j<10){
                            Node detailNode = itemChilds.item(j);
                            if(detailNode.getNodeType() != Node.TEXT_NODE) {                                
                                if(detailNode.getNodeName().equalsIgnoreCase("title")) {
                                    ttl = new LabelField(getNodeValue(detailNode)) {
                                        public void paint(Graphics g) {
                                            g.setColor(Color.BLUE);
                                            super.paint(g);
                                        }
                                    };
                                    from = new LabelField(r.getString(1), LabelField.FIELD_RIGHT|LabelField.USE_ALL_WIDTH);
                                    ttl.setFont(Font.getDefault().derive(Font.BOLD));
                                    from.setFont(Font.getDefault().derive(Font.BOLD));
                                    add (from);
                                    add (ttl);
                                } else if(detailNode.getNodeName().equalsIgnoreCase("description")) {

                                    desc = new LabelField(getNodeValue(detailNode), 0, 70, USE_ALL_WIDTH);
                                    add(desc);
                                } else if(detailNode.getNodeName().equalsIgnoreCase("dc:date")) {
                                    date = new LabelField(getNodeValue(detailNode), 11, 5, USE_ALL_WIDTH) {
                                        public void paint(Graphics g) {
                                            g.setColor(Color.ORANGE);
                                            super.paint(g);
                                        }
                                    };
                                    add(date);
                                    add(new SeparatorField());
                                } else if(detailNode.getNodeName().equalsIgnoreCase("pubDate")) {
                                    date = new LabelField(getNodeValue(detailNode), 0, 22, USE_ALL_WIDTH) {
                                        public void paint(Graphics g) {
                                            g.setColor(Color.ORANGE);
                                            super.paint(g);
                                        }
                                    };
                                    add(date);
                                    add(new SeparatorField());
                                } else {
                                    System.out.println("not the node");
                                }
                            } else {
                                System.out.println("not text node");
                            }
                            j++;
                        }
                    }
                    i++;
                    BinNews bin = new BinNews();
                    bin.setProv(from.getText());
                    bin.setTitle(ttl.getText());
                    bin.setDesc(desc.getText());
                    bin.setDate(date.getText());
                    binN.addElement(bin);
                }
                setBinN(binN);
            }
            //setBinN(binN);
            st.close();
            d.close();
        } catch (Exception e)  {
            add (new LabelField(e.toString(),LabelField.HCENTER|LabelField.USE_ALL_WIDTH));
            System.out.println(e.toString());
        }
    }

    public String getNodeValue(Node node) {
        NodeList nodeList = node.getChildNodes();
        Node childNode = nodeList.item(0);
        return childNode.getNodeValue();
    }
}

我尝试将一个名为 BinNews 的对象中的所有数据存储到一个名为 binN 的向量中。但是当我调试时,我发现 BinN 有空值,因为“binN.addElement(bin)”不起作用。请指教。

4

1 回答 1

0

首先,直到 while(i < 10) 循环完成后,您才真正调用 setBinN。因此,当您说 binN.addElement(bin) 时, binN 将为空。

但是,您的 setBinN(binN) 调用没有意义,因为您传入 binN 然后将其设置为自身,这不会做任何事情。

你可以做的是binN = new Vector();在构造函数的顶部,然后它以后不会为空。如果您将 Bi​​nNews 对象直接添加到 binN,我认为以后不需要调用 setBinN。

于 2012-06-05T02:56:06.950 回答