获取包含在<image>标签中的字符串,其值为RSS项 – Android

huangapple go评论74阅读模式
英文:

Get String In <image> Tags With getValue of RSS Item - Android

问题

else if (qName.equalsIgnoreCase("image")) { //NEW ADDITION
    currentState = State.media;
    String attrValue = attributes.getValue("WHAT");
    item.setThumburl(attrValue);
} else if (qName.equalsIgnoreCase("media:thumbnail")) {
    currentState = State.media;    
    String attrValue = attributes.getValue("url");
    item.setThumburl(attrValue);
} else if (qName.equalsIgnoreCase("media:content")) {
    currentState = State.media;   
    String attrValue = attributes.getValue("url");
    if (attributes.getValue("type") == null || attributes == null){
        return;
    } else if (attributes.getValue("type").startsWith("image")){
        item.setThumburl(attrValue);
    } else if (attributes.getValue("type").startsWith("video")){ 
        item.setVideourl(attrValue);
    } else if (attributes.getValue("type").startsWith("audio")){ 
        item.setAudiourl(attrValue);
    } 
} else if (qName.equalsIgnoreCase("enclosure")){
    currentState = State.media;   
    String attrValue = attributes.getValue("url");
    if (attributes == null || attributes.getValue("type") == null) {
        return;
    } else if (attributes.getValue("type").startsWith("image")){
        item.setThumburl(attrValue);
    } else if (attributes.getValue("type").startsWith("video")){ 
        item.setVideourl(attrValue);
    } else if (attributes.getValue("type").startsWith("audio")){ 
        item.setAudiourl(attrValue);
    } 
}

注意:这只是给出了代码部分的翻译,不包括注释或其他文本。如果你需要完整的翻译,请在需要的地方添加适当的翻译代码。

英文:

How can i get the string in &lt;image&gt; tags with getValue?&lt;image&gt;https://i2.cnnturk.com/i/cnnturk/75/720x490/5e6f7a9a70380e0cc8f49313&lt;/image&gt;

i need https://i2.cnnturk.com/i/cnnturk/75/720x490/5e6f7a9a70380e0cc8f49313 as value.

I'm able to show image, if it's &lt;media:content url=&quot;&quot;/&gt; &lt;media:thumbnail url=&quot;&quot;/&gt; &lt;enclosure url=&quot;&quot;/&gt; and need to show &lt;image&gt; too.

Also is there any way to format RSS publish date GMT to GMT+3? I need to show as different timezone.

RSS Item:

&lt;item&gt;
&lt;guid isPermaLink=&quot;true&quot;&gt;5e6f79fe70380e0cc8f49311&lt;/guid&gt;
&lt;link&gt;https://www.cnnturk.com/turkiye/istanbul-barosundan-koronaviruse-karsi-avukatlara-acil-mesaj&lt;/link&gt;
&lt;title&gt;
&lt;![CDATA[ İstanbul Barosu&amp;#39;ndan koronavir&#252;s&amp;#39;e karşı avukatlara &amp;#39;acil&amp;#39; mesaj ]]&gt;
&lt;/title&gt;
&lt;description&gt;
&lt;![CDATA[ İstanbul Barosu Başkanlığı Koronavir&#252;s nedeniyle &amp;quot;Acil&amp;quot; başlığıyla &#252;ye avukatlara mesaj attı. Mesajda duruşmaların 14 Nisan 2020 tarihinden sonra g&#246;r&#252;leceği, avukatlara bu s&#252;re&#231;te dava a&#231;ılmaması &#246;nerildi. ]]&gt;
&lt;/description&gt;
&lt;pubDate&gt;Mon, 16 Mar 2020 16:09:49 GMT&lt;/pubDate&gt;
&lt;atom:link href=&quot;https://www.cnnturk.com/turkiye/istanbul-barosundan-koronaviruse-karsi-avukatlara-acil-mesaj&quot;/&gt;
&lt;image&gt;https://i2.cnnturk.com/i/cnnturk/75/720x490/5e6f7a9a70380e0cc8f49313&lt;/image&gt;
&lt;/item&gt;

Here is android code:

Need help for

else if (qName.equalsIgnoreCase(&quot;image&quot;)) { //NEW ADDITION
currentState = State.media;
String attrValue = attributes.getValue(&quot;WHAT&quot;);
item.setThumburl(attrValue);

_FULL CODE

@Override
public void startElement(final String uri, final String localName, 
final String qName, final Attributes attributes)
throws SAXException {
currentState = State.unknown;
tagContent = new StringBuilder();
if (localName.equalsIgnoreCase(&quot;item&quot;) || localName.equalsIgnoreCase(&quot;entry&quot;)) {
itemFound = true;
item = new RSSItem();
currentState = State.unknown;
} else if (qName.equalsIgnoreCase(&quot;title&quot;)) {
currentState = State.title;
} else if (qName.equalsIgnoreCase(&quot;description&quot;) || qName.equalsIgnoreCase(&quot;content:encoded&quot;) || qName.equalsIgnoreCase(&quot;content&quot;)) {
currentState = State.description;
} else if (qName.equalsIgnoreCase(&quot;link&quot;) || qName.equalsIgnoreCase(&quot;origLink&quot;)) {
currentState = State.link;
} else if (qName.equalsIgnoreCase(&quot;pubdate&quot;) || qName.equalsIgnoreCase(&quot;published&quot;)) {
currentState = State.pubdate;
} else if (qName.equalsIgnoreCase(&quot;image&quot;)) { //NEW ADDITION
currentState = State.media;
String attrValue = attributes.getValue(&quot;WHAT&quot;);
item.setThumburl(attrValue);
} else if (qName.equalsIgnoreCase(&quot;media:thumbnail&quot;)) {
currentState = State.media;    
String attrValue = attributes.getValue(&quot;url&quot;);
item.setThumburl(attrValue);
} else if (qName.equalsIgnoreCase(&quot;media:content&quot;)){
currentState = State.media;   
String attrValue = attributes.getValue(&quot;url&quot;);
if (attributes.getValue(&quot;type&quot;) == null || attributes == null){
return;
} else if (attributes.getValue(&quot;type&quot;).startsWith(&quot;image&quot;)){
item.setThumburl(attrValue);
} else if (attributes.getValue(&quot;type&quot;).startsWith(&quot;video&quot;)){ 
item.setVideourl(attrValue);
} else if (attributes.getValue(&quot;type&quot;).startsWith(&quot;audio&quot;)){ 
item.setAudiourl(attrValue);
} 
} else if (qName.equalsIgnoreCase(&quot;enclosure&quot;)){
currentState = State.media;   
String attrValue = attributes.getValue(&quot;url&quot;);
if (attributes == null || attributes.getValue(&quot;type&quot;) == null) {
return;
} else if (attributes.getValue(&quot;type&quot;).startsWith(&quot;image&quot;)){
item.setThumburl(attrValue);
} else if (attributes.getValue(&quot;type&quot;).startsWith(&quot;video&quot;)){ 
item.setVideourl(attrValue);
} else if (attributes.getValue(&quot;type&quot;).startsWith(&quot;audio&quot;)){ 
item.setAudiourl(attrValue);
} 
}
}

答案1

得分: 0

你可以尝试使用正则表达式 \&lt;(\Qimage\E)\&gt;(.*?)\&lt;\/\1\&gt; 来匹配相应的字符。它可以匹配所有图像标签的内容。

public static void main(String[] args){
    String s = "&lt;image&gt;https://x.com/image.jpg&lt;/image&gt;"
                + "&lt;image&gt;https://x.com/image1.jpg&lt;/image&gt;"
                + "&lt;image&gt;https://x.com/image2.jpg&lt;/image&gt;";
    Pattern p = Pattern.compile("\\&lt;(\\Qimage\\E)\\&gt;(.*?)\\&lt;\\/\\1\\&gt;");
    Matcher m = p.matcher(s);
    while(m.find()){
        System.out.println(m.group(2));
    }
}

如果你不想使用正则表达式:

int record = 0;
while(record != -1){
    int a = qName.indexOf("&lt;image&gt;", record);
    if(a == -1){
        break;
    }
    int b = qName.indexOf("&lt;/image&gt;", a);
    String url = qName.substring(a + 7, b + 8);
    System.out.println(url);
}
英文:

You can try the regular expression \&lt;(\Qimage\E)\&gt;(.*?)\&lt;\/\1\&gt;To match the corresponding characters.It can match contents of all image labels.

public static void main(String[] args){
String s=&quot;&lt;image&gt;https://x.com/image.jpg&lt;/image&gt;&quot;
+ &quot;&lt;image&gt;https://x.com/image1.jpg&lt;/image&gt;&quot;
+ &quot;&lt;image&gt;https://x.com/image2.jpg&lt;/image&gt;&quot;;
Pattern p = Pattern.compile(&quot;\\&lt;(\\Qimage\\E)\\&gt;(.*?)\\&lt;\\/\\1\\&gt;&quot;);
Matcher m = p.matcher(s);
while(m.find()){
System.out.println(m.group(2));
}
}

Update: I just updated the code. I'm sorry I wrote it wrong before.

if you don't want to use the regular expression

int record = 0;
while(record!=-1){
int a = qName.indexOf(&quot;&lt;image&gt;&quot;,record);
if(a==-1){
break;
}
int b = qName.indexOf(&quot;&lt;/image&gt;&quot;,a);
String url=qName.substring(a+7,b+8);
System.out.println(url);

}

答案2

得分: 0

String sete = "<image>https://x.com/a.jpg</image>";
String sete2 = sete.replace("<image>", "");
英文:
String sete = &quot;&lt;image&gt;https://x.com/a.jpg&lt;/image&gt;&quot;;
String sete2 = sete.replace(&quot;&lt;image&gt;&quot;, &quot;&quot;);

huangapple
  • 本文由 发表于 2020年3月16日 22:44:04
  • 转载请务必保留本文链接:https://go.coder-hub.com/60708042.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定