使用java DocumentBuilder从xml feed获取rel =“alternate”href值

时间:2011-03-27 21:14:17

标签: java android xml

需要你的帮助......确保它对于Xmls开发人员的体验来说看起来像一个愚蠢的问题

我正在尝试阅读以下Feed:

http://feeds.feedburner.com/Orangette

使用InputStream和Document来解析xml

like:Document dom = db.parse(in);

这是条目elemet的样子:

<entry>
<id>tag:blogger.com,1999:blog-7793856.post-7139617615025474381</id>
<published>2011-03-21T10:42:00.000-07:00</published>
<updated>2011-03-21T10:44:11.297-07:00</updated>
<title type="text">It's called the Pantry</title>
<summary type="html">Well. World events don’t seem to get any less troubling, so we might as well get back to business.  Yes?Last week, I said that I wanted to tell you about a new project, and I still do.  It’s a project that grows out of Delancey, but it’s a whole new thing: a business headed up by two of our friends, Brandi Henderson and Olaiya Land. Brandon is technically the third partner, but this baby really &lt;img src="http://feeds.feedburner.com/~r/Orangette/~4/X6Tm2_LpxYI" height="1" width="1"/&gt;</summary>
<link rel="replies" type="application/atom+xml" href="http://orangette.blogspot.com/feeds/7139617615025474381/comments/default" title="Post Comments" />
<link rel="replies" type="text/html" href="https://www.blogger.com/comment.g?blogID=7793856&amp;postID=7139617615025474381" title="163 Comments" />
<link rel="edit" type="application/atom+xml" href="http://www.blogger.com/feeds/7793856/posts/default/7139617615025474381" />
<link rel="self" type="application/atom+xml" href="http://www.blogger.com/feeds/7793856/posts/default/7139617615025474381" />
<link rel="alternate" type="text/html" href="http://orangette.blogspot.com/2011/03/its-called-pantry.html" title="It's called the Pantry" />
<author>
  <name>Molly</name>
  <uri>http://www.blogger.com/profile/01493708300940204826</uri>
  <email>cheeseandchocolate@gmail.com</email>
  <gd:extendedProperty xmlns:gd="http://schemas.google.com/g/2005" name="OpenSocialUserId" value="07818414465072164731" />
</author>
<media:thumbnail xmlns:media="http://search.yahoo.com/mrss/" url="http://4.bp.blogspot.com/-G2usp64xroc/TYaQ0wdcaUI/AAAAAAAAC_o/L4l6deIvm6U/s72-c/5544384475_858400a1f7_z.jpg" height="72" width="72" />
<thr:total>163</thr:total></entry>

所有工作良好,除了只获得一个rel和take是href值

在这种情况下,备用rel是相关的

<link rel="alternate" type="text/html" href="http://orangette.blogspot.com/2011/03/its-called pantry.html" title="It's called the Pantry" />

浪费了很多时间试图让他离开那里..但是nada,很乐意获得任何线索..

1 个答案:

答案 0 :(得分:1)

好的,找到我自己的解决方案100%确定有更清洁的方式,但是......

因为我的应用程序支持低级别的android平台我无法使用8级支持的xpath(2.2)

我的解决方案是:

 public static String getTagValueWithMultiItem(Element eElement){
                 String returnVal = "" ;
                 Node eNode ;
                 int NumOFItem = eElement.getElementsByTagName("link").getLength();
                 for (int y = 0; y < NumOFItem; y++) {
                 eNode = eElement.getElementsByTagName("link").item(y);
                 NamedNodeMap attributes = eNode.getAttributes();
                  for (int g = 0; g < attributes.getLength(); g++) {
                      Attr attribute = (Attr)attributes.item(g);
                                 if(attribute.getNodeName().equals("rel")&&attribute.getNodeValue().equals("alternate"))
                                    {
                                      try { 
                                            returnVal =eNode.getAttributes().getNamedItem("href").getNodeValue();
                                           }  
                                         catch (Exception e) {
                                            returnVal = e.toString();
                                         }
                                     } 
                                  }
                 }
                return returnVal;    
             }

任何进步都会有所改善