android xml解析不显示结果?

时间:2013-07-09 13:58:26

标签: android xml-parsing

我是android的新手,我正在尝试解析来自http://www.astrology.com/horoscopes/daily-horoscope.rss的xml数据,我从中抓取代码 How can I parse xml from url in android?在这里,我使用了sherLock库,我还在清单中设置了互联网权限,但仍然无法抓取它,除了一点修改之外,这里是相同的代码,

protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    /* Create a new layout to display the view */
    LinearLayout layout = new LinearLayout(this);
    layout.setOrientation(1);

    /* Create a new textview array to display the results */
    TextView name[];
    //TextView website[];
    //TextView category[];

    try {

        URL url = new URL("http://www.astrology.com/horoscopes/daily-horoscope.rss");
        DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
        DocumentBuilder db = dbf.newDocumentBuilder();
        Document doc = db.parse(new InputSource(url.openStream()));
        doc.getDocumentElement().normalize();

        NodeList nodeList = doc.getElementsByTagName("item");

        /** Assign textview array lenght by arraylist size */
        name = new TextView[nodeList.getLength()];
       // website = new TextView[nodeList.getLength()];
        //category = new TextView[nodeList.getLength()];

        for (int i = 0; i < nodeList.getLength(); i++) {

            Node node = nodeList.item(i);

            name[i] = new TextView(this);
            //website[i] = new TextView(this);
            //category[i] = new TextView(this);

            Element fstElmnt = (Element) node;
            NodeList nameList = ((Document) fstElmnt).getElementsByTagName("title");
            Element nameElement = (Element) nameList.item(0);
            nameList = ((Node) nameElement).getChildNodes();
            name[i].setText("Name = " + ((Node) nameList.item(0)).getNodeValue());

            //category[i].setText("Website Category = " + ((org.w3c.dom.Element) websiteElement).getAttribute("category"));

            layout.addView(name[i]);
            //layout.addView(website[i]);
            //layout.addView(category[i]);
        }
    } catch (Exception e) {
        System.out.println("XML Pasing Excpetion = " + e);
    }

    /** Set the layout view to display */
    setContentView(layout);

}

请记住,我正在从SherlockActivity扩展我的MainActivity类,当我执行代码时,只显示操作栏!

0 个答案:

没有答案