开发者

XML parser gives null element

When I try to parse a XML-file, it gives sometimes a null element by the title. I think it has to do with HTML-tags '

How can I solve this problem?

I have the follow XML-file:

<item>
<title>&#039; Nieuwe DVD &#039;</title>
<description>tekst, tekst tekst&l开发者_如何学JAVAt;/description>
<link>dvd.html</link>
<category>nieuws</category>
<pubDate>Sat, 1 Jan 2011 9:24:00 +0000</pubDate>
</item>

And the follow code to parse the xml-file:

//DocumentBuilderFactory, DocumentBuilder are used for 
      //xml parsing
      DocumentBuilderFactory dbf = DocumentBuilderFactory
        .newInstance();
      DocumentBuilder db = dbf.newDocumentBuilder();

      //using db (Document Builder) parse xml data and assign
      //it to Element
      Document document = db.parse(is);
      Element element = document.getDocumentElement();

      //take rss nodes to NodeList
      element.normalize();

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

      if (nodeList.getLength() > 0) 
      {
       for (int i = 0; i < nodeList.getLength(); i++) 
       {
        //take each entry (corresponds to <item></item> tags in 
        //xml data

        Element entry = (Element) nodeList.item(i);
        entry.normalize();
        Element _titleE = (Element) entry.getElementsByTagName(
          "title").item(0);

        Element _categoryE = (Element) entry
          .getElementsByTagName("category").item(0);
        Element _pubDateE = (Element) entry
          .getElementsByTagName("pubDate").item(0);
        Element _linkE = (Element) entry.getElementsByTagName(
          "link").item(0);

        String _title = _titleE.getFirstChild().getNodeValue();
        String _category = _categoryE.getFirstChild().getNodeValue();
        Date _pubDate = new Date(_pubDateE.getFirstChild().getNodeValue());
        String _link = _linkE.getFirstChild().getNodeValue();

        //create RssItemObject and add it to the ArrayList
        RssItem rssItem = new RssItem(_title, _category, _pubDate, _link);

        rssItems.add(rssItem);
        conn.disconnect();
       }


Don't use getFirstElement when you really want getTextContent.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜