【问题标题】:How to parse rss-feed with SAX, other methods to parse?如何用 SAX 解析 rss-feed,其他解析方法?
【发布时间】:2012-08-23 10:35:12
【问题描述】:

我正在尝试解析多个属性但没有成功。

这些属性我解析得很好:

<id>..</id>
<published>..</published>
<content>..</content>
<title>..</title>

我解析的 xml 图像和我需要的数据: 但我需要用标签获取属性:

<media:thumbnail url='http://ww.../> </media:thumbnail>
<media:content url='http://ww.../> </<media:content>

我在某处读到 sax 解析不支持这样做,我当时不明白这是真的。

我使用代码是为了执行解析:

public class YoutubePARSER {
    private static final String LOGTAG = "LogsAndroid";
    private ArrayList<YoutubeVO> videos = new ArrayList<YoutubeVO>();
    private YoutubeVO video;
    private URL rssUrl;
    final String myString = "http://www.w3.org/2005/Atom";

    SAXParserFactory parser = SAXParserFactory.newInstance(); 

public YoutubePARSER(String url)
{
    try
    {
        this.rssUrl = new URL(url);
    }
    catch (MalformedURLException e)
    {
        throw new RuntimeException(e);
    } 
}

public ArrayList<YoutubeVO> parse()
{
    RootElement root = new RootElement(myString, "feed");
          Element itemPlace = root.getChild(myString, "entry");
          itemPlace.setStartElementListener(new StartElementListener(){
              public void start(Attributes attrs){
                  video = new YoutubeVO();  
              }
          });

      itemPlace.setEndElementListener(new EndElementListener(){
            public void end() {
                videos.add(video);
            }
        });

      itemPlace.getChild(myString, "id").setEndTextElementListener(
              new EndTextElementListener(){
                  public void end(String body) {
                      video.setId(body);
                  }
          });

      itemPlace.getChild(myString, "published").setEndTextElementListener(
    new EndTextElementListener(){
        public void end(String body) {
            video.setPublished(body);
        }
});

我尝试通过以下方式解析该字段,但没有成功:

 itemPlace.getChild(myString, "media:thumbnail").setEndTextElementListener(
 itemPlace.getChild(myString, "media").setEndTextElementListener(
 itemPlace.getChild(myString, "thumbnail").setEndTextElementListener(
 itemPlace.getChild(myString, "media:thumbnail url").setEndTextElementListener(

有什么办法吗??

谢谢!

【问题讨论】:

    标签: android parsing rss


    【解决方案1】:

    为了能够读取“media:content”或任何其他“media:”元素,您需要指定媒体Namespace .

    您在代码中设置了错误的名称空间并使用了 myString="http://www.w3.org/2005/Atom"

    itemPlace.getChild(myString, "thumbnail").setEndTextElementListener(
    

    将其更改为正确的命名空间会很有效:

    itemPlace.getChild("http://search.yahoo.com/mrss/", "thumbnail")setElementListener(new ElementListener() {
            @Override
            public void end() {
    
            }
    
            @Override
            public void start(Attributes attributes) {
                String urlAttr = attributes.getValue("url");
            }
        });
    

    注意:您无需指定命名空间前缀,只需使用元素名称(在本例中为缩略图)。

    【讨论】:

      【解决方案2】:

      我的解决方案:

      public class DomParserSampleActivity extends Activity {
          @Override
          public void onCreate(Bundle savedInstanceState) {
              super.onCreate(savedInstanceState);
      
              ScrollView mScrView1 = new ScrollView(this);
      
              /** 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 id[];
              TextView published[];
              TextView content[];
              TextView title[];
      
              TextView mediacontent[];
              TextView mediathumbnail[];
      
              try {
                  URL url = new URL(
                          "http://gdata.youtube.com/feeds/api/users/estudiosabiertostv/uploads");
                  DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
                  DocumentBuilder db = dbf.newDocumentBuilder();
                  Document doc = db.parse(new InputSource(url.openStream()));
                  doc.getDocumentElement().normalize();
      
                  NodeList nodeList = doc.getElementsByTagName("entry");
      
                  /** Assign textview array length by arraylist size */
                  id = new TextView[nodeList.getLength()];
                  published = new TextView[nodeList.getLength()];
                  content = new TextView[nodeList.getLength()];
                  title = new TextView[nodeList.getLength()];
                  mediacontent = new TextView[nodeList.getLength()];
                  mediathumbnail = new TextView[nodeList.getLength()];
      
                  for (int i = 0; i < nodeList.getLength(); i++) {
      
                      Node node = nodeList.item(i);
      
                      id[i] = new TextView(this);
                      published[i] = new TextView(this);
                      content[i] = new TextView(this);
                      title[i] = new TextView(this);
      
                      Element fstElmnt = (Element) node;
      
                      NodeList idList = fstElmnt.getElementsByTagName("id");
                      Element idElement = (Element) idList.item(0);
                      idList = idElement.getChildNodes();
                      id[i].setText("Id is = "
                              + ((Node) idList.item(0)).getNodeValue());
      
                      Log.v("TAG","id: "+idList.item(0).getNodeValue());
      
                      NodeList publishedList = fstElmnt
                              .getElementsByTagName("published");
                      Element publishedElement = (Element) publishedList.item(0);
                      publishedList = publishedElement.getChildNodes();
                      published[i].setText("published is = "
                              + ((Node) publishedList.item(0)).getNodeValue());
      
                      Log.v("TAG","published: "+publishedList.item(0).getNodeValue());
      
                      NodeList contentList = fstElmnt.getElementsByTagName("content");
                      Element contentElement = (Element) contentList.item(0);
                      contentList = contentElement.getChildNodes();
                      content[i].setText("content is = "
                              + ((Node) contentList.item(0)).getNodeValue());
      
                      Log.v("TAG","content: "+contentList.item(0).getNodeValue());
      
                      NodeList titleList = fstElmnt.getElementsByTagName("title");
                      Element titleElement = (Element) titleList.item(0);
                      titleList = titleElement.getChildNodes();
                      title[i].setText("title is = "
                              + ((Node) titleList.item(0)).getNodeValue());
      
                      Log.v("TAG","titulo: "+titleList.item(0).getNodeValue());
      
                      NodeList nodeList1 = fstElmnt
                              .getElementsByTagName("media:group");
      
                      for (int j = 0; j < nodeList1.getLength(); j++) {
                          Node node1 = nodeList1.item(j);
                          mediacontent[j] = new TextView(this);
                          mediathumbnail[j] = new TextView(this);
      
                          Element secondElmnt = (Element) node1;
      
                          NodeList mediacontentList = secondElmnt
                                  .getElementsByTagName("media:content");
                          Element mediacontentElement = (Element) mediacontentList
                                  .item(0);
                          mediacontent[j].setText("mediacontent url is = "
                                  + mediacontentElement.getAttribute("url"));
      
                          Log.v("TAG","MEDIACONTENT: "+mediacontentElement.getAttribute("url"));
      
                          NodeList mediathumbnailList = secondElmnt
                                  .getElementsByTagName("media:thumbnail");
                          Element mediathumbnailElement = (Element) mediathumbnailList
                                  .item(0);
                          mediathumbnail[j].setText("mediathumbnail url is = "
                                  + mediathumbnailElement.getAttribute("url"));
      
                          Log.v("TAG","MEDIATHUMBNAIL: "+mediathumbnailElement.getAttribute("url"));
      
                          layout.addView(mediacontent[j]);
                          layout.addView(mediathumbnail[j]);
                      }
      
                      layout.addView(id[i]);
                      layout.addView(published[i]);
                      layout.addView(content[i]);
                      layout.addView(title[i]);
      
                  }
              } catch (Exception e) {
                  e.printStackTrace();
              }
      
              /** Set the layout view to display */
      
              mScrView1.addView(layout);
              setContentView(mScrView1);
          }
      }
      

      不要忘记在线程中这样做或失败,请使用这些行 在 onCreate() 之后

          StrictMode.ThreadPolicy policy = new StrictMode.ThreadPolicy.Builder()
      .permitAll().build();
          StrictMode.setThreadPolicy(policy);
      

      【讨论】:

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