gpt4 book ai didi

java - 使用 RSS 项目的 getValue 获取 标签中的字符串 - Android

转载 作者:行者123 更新时间:2023-12-01 17:56:28 24 4
gpt4 key购买 nike

如何获取 <image> 中的字符串带有 getValue 的标签? <image>https://i2.cnnturk.com/i/cnnturk/75/720x490/5e6f7a9a70380e0cc8f49313</image>

我需要https://i2.cnnturk.com/i/cnnturk/75/720x490/5e6f7a9a70380e0cc8f49313作为值。

我能够显示图像,如果它是 <media:content url=""/> <media:thumbnail url=""/> <enclosure url=""/>并且需要显示<image>也是。

还有什么方法可以将 RSS 发布日期 GMT 格式化为 GMT+3?我需要显示为不同的时区。

RSS 项目:

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

这是 Android 代码:

需要帮助

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

_完整代码

@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("item") || localName.equalsIgnoreCase("entry")) {
itemFound = true;
item = new RSSItem();
currentState = State.unknown;
} else if (qName.equalsIgnoreCase("title")) {
currentState = State.title;
} else if (qName.equalsIgnoreCase("description") || qName.equalsIgnoreCase("content:encoded") || qName.equalsIgnoreCase("content")) {
currentState = State.description;
} else if (qName.equalsIgnoreCase("link") || qName.equalsIgnoreCase("origLink")) {
currentState = State.link;
} else if (qName.equalsIgnoreCase("pubdate") || qName.equalsIgnoreCase("published")) {
currentState = State.pubdate;
} 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);
}
}

}

最佳答案

您可以尝试正则表达式 \<(\Qimage\E)\>(.*?)\<\/\1\>匹配相应的字符。可以匹配所有图像标签的内容。

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

更新:我刚刚更新了代码。抱歉之前写错了。

如果您不想使用正则表达式

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

}

关于java - 使用 RSS 项目的 getValue 获取 <image> 标签中的字符串 - Android,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60708042/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com