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

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

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

问题

  1. else if (qName.equalsIgnoreCase("image")) { //NEW ADDITION
  2. currentState = State.media;
  3. String attrValue = attributes.getValue("WHAT");
  4. item.setThumburl(attrValue);
  5. } else if (qName.equalsIgnoreCase("media:thumbnail")) {
  6. currentState = State.media;
  7. String attrValue = attributes.getValue("url");
  8. item.setThumburl(attrValue);
  9. } else if (qName.equalsIgnoreCase("media:content")) {
  10. currentState = State.media;
  11. String attrValue = attributes.getValue("url");
  12. if (attributes.getValue("type") == null || attributes == null){
  13. return;
  14. } else if (attributes.getValue("type").startsWith("image")){
  15. item.setThumburl(attrValue);
  16. } else if (attributes.getValue("type").startsWith("video")){
  17. item.setVideourl(attrValue);
  18. } else if (attributes.getValue("type").startsWith("audio")){
  19. item.setAudiourl(attrValue);
  20. }
  21. } else if (qName.equalsIgnoreCase("enclosure")){
  22. currentState = State.media;
  23. String attrValue = attributes.getValue("url");
  24. if (attributes == null || attributes.getValue("type") == null) {
  25. return;
  26. } else if (attributes.getValue("type").startsWith("image")){
  27. item.setThumburl(attrValue);
  28. } else if (attributes.getValue("type").startsWith("video")){
  29. item.setVideourl(attrValue);
  30. } else if (attributes.getValue("type").startsWith("audio")){
  31. item.setAudiourl(attrValue);
  32. }
  33. }

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

英文:

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:

  1. &lt;item&gt;
  2. &lt;guid isPermaLink=&quot;true&quot;&gt;5e6f79fe70380e0cc8f49311&lt;/guid&gt;
  3. &lt;link&gt;https://www.cnnturk.com/turkiye/istanbul-barosundan-koronaviruse-karsi-avukatlara-acil-mesaj&lt;/link&gt;
  4. &lt;title&gt;
  5. &lt;![CDATA[ İstanbul Barosu&amp;#39;ndan koronavir&#252;s&amp;#39;e karşı avukatlara &amp;#39;acil&amp;#39; mesaj ]]&gt;
  6. &lt;/title&gt;
  7. &lt;description&gt;
  8. &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&#231lmaması &#246;nerildi. ]]&gt;
  9. &lt;/description&gt;
  10. &lt;pubDate&gt;Mon, 16 Mar 2020 16:09:49 GMT&lt;/pubDate&gt;
  11. &lt;atom:link href=&quot;https://www.cnnturk.com/turkiye/istanbul-barosundan-koronaviruse-karsi-avukatlara-acil-mesaj&quot;/&gt;
  12. &lt;image&gt;https://i2.cnnturk.com/i/cnnturk/75/720x490/5e6f7a9a70380e0cc8f49313&lt;/image&gt;
  13. &lt;/item&gt;

Here is android code:

Need help for

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

_FULL CODE

  1. @Override
  2. public void startElement(final String uri, final String localName,
  3. final String qName, final Attributes attributes)
  4. throws SAXException {
  5. currentState = State.unknown;
  6. tagContent = new StringBuilder();
  7. if (localName.equalsIgnoreCase(&quot;item&quot;) || localName.equalsIgnoreCase(&quot;entry&quot;)) {
  8. itemFound = true;
  9. item = new RSSItem();
  10. currentState = State.unknown;
  11. } else if (qName.equalsIgnoreCase(&quot;title&quot;)) {
  12. currentState = State.title;
  13. } else if (qName.equalsIgnoreCase(&quot;description&quot;) || qName.equalsIgnoreCase(&quot;content:encoded&quot;) || qName.equalsIgnoreCase(&quot;content&quot;)) {
  14. currentState = State.description;
  15. } else if (qName.equalsIgnoreCase(&quot;link&quot;) || qName.equalsIgnoreCase(&quot;origLink&quot;)) {
  16. currentState = State.link;
  17. } else if (qName.equalsIgnoreCase(&quot;pubdate&quot;) || qName.equalsIgnoreCase(&quot;published&quot;)) {
  18. currentState = State.pubdate;
  19. } else if (qName.equalsIgnoreCase(&quot;image&quot;)) { //NEW ADDITION
  20. currentState = State.media;
  21. String attrValue = attributes.getValue(&quot;WHAT&quot;);
  22. item.setThumburl(attrValue);
  23. } else if (qName.equalsIgnoreCase(&quot;media:thumbnail&quot;)) {
  24. currentState = State.media;
  25. String attrValue = attributes.getValue(&quot;url&quot;);
  26. item.setThumburl(attrValue);
  27. } else if (qName.equalsIgnoreCase(&quot;media:content&quot;)){
  28. currentState = State.media;
  29. String attrValue = attributes.getValue(&quot;url&quot;);
  30. if (attributes.getValue(&quot;type&quot;) == null || attributes == null){
  31. return;
  32. } else if (attributes.getValue(&quot;type&quot;).startsWith(&quot;image&quot;)){
  33. item.setThumburl(attrValue);
  34. } else if (attributes.getValue(&quot;type&quot;).startsWith(&quot;video&quot;)){
  35. item.setVideourl(attrValue);
  36. } else if (attributes.getValue(&quot;type&quot;).startsWith(&quot;audio&quot;)){
  37. item.setAudiourl(attrValue);
  38. }
  39. } else if (qName.equalsIgnoreCase(&quot;enclosure&quot;)){
  40. currentState = State.media;
  41. String attrValue = attributes.getValue(&quot;url&quot;);
  42. if (attributes == null || attributes.getValue(&quot;type&quot;) == null) {
  43. return;
  44. } else if (attributes.getValue(&quot;type&quot;).startsWith(&quot;image&quot;)){
  45. item.setThumburl(attrValue);
  46. } else if (attributes.getValue(&quot;type&quot;).startsWith(&quot;video&quot;)){
  47. item.setVideourl(attrValue);
  48. } else if (attributes.getValue(&quot;type&quot;).startsWith(&quot;audio&quot;)){
  49. item.setAudiourl(attrValue);
  50. }
  51. }
  52. }

答案1

得分: 0

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

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

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

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

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.

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

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

if you don't want to use the regular expression

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

}

答案2

得分: 0

  1. String sete = "<image>https://x.com/a.jpg</image>";
  2. String sete2 = sete.replace("<image>", "");
英文:
  1. String sete = &quot;&lt;image&gt;https://x.com/a.jpg&lt;/image&gt;&quot;;
  2. 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:

确定