0

これは、データを抽出したいRSS、XMLファイルです。私は SAX パーサーを使用してすべての作業を完了しました。すべて正常に動作します。しかし、RSSファイル全体を解析し、 のような各要素データを取得しました。

しかし、私の問題は、ファイル要素データ全体を取得していることですが、含まれている要素のみが必要です。

<rss version="2.0">
<channel>
<title>RSS Feed</title>
<link>http://www.xyz.com</link>
<description>Calendar RSS Feeds</description>
<language>en-us</language>
<ttl>60</ttl>
<item>
<title>
title 1
</title>
<description>description 1</description>
<link>
http://www.xyz.com
</link>
<guid isPermaLink="false">6027@http://www.xyz.com</guid>
</item>
<item>
<title>
title 2
</title>
<description>description 2</description>
<link>
http://www.xyz.com
</link>
<guid isPermaLink="false">5554@http://www.xyz.com</guid>
</item>
<item>
</channel>
</rss>

これを設定するためにすべての組み合わせを試しました。ここでコードを示します。startElement および endElement メソッドを作成しました。

public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        // TODO Auto-generated method stub

        current = true;

        if (localName.equals("channel"))
        {
            itemList = new ItemList();
        }
    }

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {
        // TODO Auto-generated method stub
        current = false;

        if(localName.equals("title"))
        {
            itemList.setTitle(currentValue);
        }

    }

    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {
        // TODO Auto-generated method stub

        if(current)
        {
            currentValue = new String(ch, start, length);
            current=false;
        }
    }

要素の下にある要素のみを取得する方法を教えてください。

私もこれを試しましたが、これは私にエラーを与えます

public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {
        // TODO Auto-generated method stub

        current = true;

        if (localName.equals("channel"))
        {
            if (localName.equals("item"))
            {
            itemList = new ItemList();
            }
        }
    }

前もって感謝します。

4

2 に答える 2

0

これを試してみてください、それが役立つことを願っています....

url = new URL("http://www.abcd.com/rss.xml");//.......
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    if(conn.getResponseCode() == HttpURLConnection.HTTP_OK){
          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
          DocumentBuilder db = dbf.newDocumentBuilder();
          Document doc;
          doc = db.parse(url.openStream());
          doc.getDocumentElement().normalize();
          NodeList itemLst = doc.getElementsByTagName("item");



          Array.Description = new String[itemLst.getLength()];//........
          Array.Title = new String[itemLst.getLength()];
          Array.image = new String[itemLst.getLength()];
          Array.Date = new String[itemLst.getLength()];

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

                Node item = itemLst.item(i);
                if(item.getNodeType() == Node.ELEMENT_NODE){
                      Element ielem = (Element)item;
                      NodeList title = ielem.getElementsByTagName("title");
                      NodeList date = ielem.getElementsByTagName("pubDate");

                      NodeList description = ielem.getElementsByTagName("description");

                    ////.........
                      Array.Title[i] = title.item(0).getChildNodes().item(0).getNodeValue();
                      Array.Description[i] = description.item(0).getChildNodes().item(0).getNodeValue();
                      Array.Date[i] = date.item(0).getChildNodes().item(0).getNodeValue();
于 2012-04-26T10:07:35.187 に答える
0
public class headlinesparser {


public static void parse(){
URL url;
try {
    url = new URL("http://www.abcd.com/taxonomy/term/19/rss.xml");//.......
    HttpURLConnection conn = (HttpURLConnection) url.openConnection();

    if(conn.getResponseCode() == HttpURLConnection.HTTP_OK){
          DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
          DocumentBuilder db = dbf.newDocumentBuilder();
          Document doc;
          doc = db.parse(url.openStream());
          doc.getDocumentElement().normalize();
          NodeList itemLst = doc.getElementsByTagName("item");



          Headlines.Description = new String[itemLst.getLength()];//........
          Headlines.Title = new String[itemLst.getLength()];
          Headlines.image = new String[itemLst.getLength()];
          Headlines.Date = new String[itemLst.getLength()];

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

                Node item = itemLst.item(i);
                if(item.getNodeType() == Node.ELEMENT_NODE){
                      Element ielem = (Element)item;
                      NodeList title = ielem.getElementsByTagName("title");
                      NodeList date = ielem.getElementsByTagName("pubDate");

                      NodeList description = ielem.getElementsByTagName("description");

                    ////.........
                      Headlines.Title[i] = title.item(0).getChildNodes().item(0).getNodeValue();
                      Headlines.Description[i] = description.item(0).getChildNodes().item(0).getNodeValue();
                      Headlines.Date[i] = date.item(0).getChildNodes().item(0).getNodeValue();

                    if (Headlines.Description[i].contains("<img ")){

                    String img  = Headlines.Description[i].substring(Headlines.Description[i].indexOf("<img "));
                    String cleanUp = img.substring(0, img.indexOf(">")+1);
                    img = img.substring(img.indexOf("src=") + 5);
                    int indexOf = img.indexOf("'");
                    if (indexOf==-1){
                        indexOf = img.indexOf("\"");
                    }
                    img = img.substring(0, indexOf);

                    //setImgLink(img);

                    Headlines.image[i]=img;

                      }




                }

          }

    }

} catch (MalformedURLException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (DOMException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (IOException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (ParserConfigurationException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
} catch (SAXException e) {
    // TODO Auto-generated catch block
    e.printStackTrace();
}

}


}
于 2012-04-26T10:22:32.683 に答える