- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
首先,我有这个 DOMParser 类;
import android.util.Log;
import java.io.IOException;
import java.io.StringReader;
import java.net.MalformedURLException;
import java.net.URL;
import javax.xml.parsers.DocumentBuilder;
import javax.xml.parsers.DocumentBuilderFactory;
import org.jsoup.Jsoup;
import org.jsoup.select.Elements;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;
import org.xml.sax.EntityResolver;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
public class DOMParser {
private RSSFeed _feed = new RSSFeed();
public RSSFeed parseXml(String xml) {
// _feed.clearList();
URL url = null;
try {
url = new URL(xml);
Log.e("THE XML", xml);
Log.e("THE URL", url.toString());
} catch (MalformedURLException e1) {
Log.e("MALFORMED EXCEPTION", "1");
e1.printStackTrace();
}
try {
// Create required instances
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
dbf.setValidating(false);
DocumentBuilder db = dbf.newDocumentBuilder();
db.setEntityResolver(new EntityResolver() {
@Override
public InputSource resolveEntity(String arg0, String arg1)
throws SAXException, IOException {
if (arg0.contains("Hibernate")) {
return new InputSource(new StringReader(""));
} else {
// TODO Auto-generated method stub
return null;
}
}
});
// Parse the xml
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
// Get all <item> tags.
NodeList nl = doc.getElementsByTagName("item");
int length = nl.getLength();
for (int i = 0; i < length; i++) {
Node currentNode = nl.item(i);
RSSItem _item = new RSSItem();
NodeList nchild = currentNode.getChildNodes();
int clength = nchild.getLength();
// Get the required elements from each Item
for (int j = 0; j < clength; j = j + 1) {
try {
Node thisNode = nchild.item(j);
String theString = null;
String nodeName = thisNode.getNodeName();
Log.e("NODE NAME", nodeName);
theString = nchild.item(j).getFirstChild().getNodeValue();
//Log.e("THE STRING", theString);
if (theString != null) {
if ("title".equals(nodeName)) {
// Node name is equals to 'title' so set the Node
// value to the Title in the RSSItem.
_item.setTitle(theString);
} else if ("description".equals(nodeName)) {
_item.setDescription(theString);
// Parse the html description to get the image url
String html = theString;
org.jsoup.nodes.Document docHtml = Jsoup
.parse(html);
Elements imgEle = docHtml.select("img");
_item.setImage(imgEle.attr("src"));
} else if ("pubDate".equals(nodeName)) {
// We replace the plus and zero's in the date with
// empty string
String formatedDate = theString.replace(" +0000",
"");
_item.setDate(formatedDate);
} else if ("link".equals(nodeName)) {
// Trying to get the URL as a string
_item.setURL(theString);
}
/*else if ("media:content".equals(nodeName)){
_item.setImage(theString);
Log.e("THE IMAGE LINK", theString);
}*/
}
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// add item to the list
_feed.addItem(_item);
}
} catch (Exception e) {
e.printStackTrace();
}
// Return the final feed once all the Items are added to the RSSFeed
// Object(_feed).
return _feed;
}
}
}
我正在尝试解析如下所示的条目;
<item>
<title><![CDATA[Oceans Full of 'Aliens' Could Be Hidden Beneath Earth's Surface, Expert Says]]></title>
<description><![CDATA[Do "aliens" exist on Earth? In a way, experts think so, and they believe that these creatures can be found thriving in massive underground oceans hidden hundreds of miles beneath the Earth's surface.]]></description>
<guid>http://www.natureworldnews.com/articles/33160/20161130/oceans-full-aliens-hidden-beneath-earths-surface-expert.htm</guid>
<link>http://www.natureworldnews.com/articles/33160/20161130/oceans-full-aliens-hidden-beneath-earths-surface-expert.htm</link>
<media:content url="http://images.natureworldnews.com/data/images/full/37450/earth-ocean.jpg" />
<media:title type="html"><![CDATA[earth ocean]]></media:title>
<media:text type="html"><![CDATA[Do "aliens" exist on Earth? In a way, experts think so, and they believe that these creatures can be found thriving in massive underground oceans hidden hundreds of miles beneath the Earth's surface.]]></media:text>
<category>
<name><![CDATA[News]]></name>
</category>
<pubDate>Wed, 30 Nov 2016 11:02:00 EST</pubDate>
</item>
<item>
<title><![CDATA[Great Barrier Reef Sees Its Worst Damage on Record]]></title>
<description><![CDATA[The Great Barrier Reef is reportedly experiencing its worst damage via coral bleaching by far in history. The culprit is none other than the significant increase in water temperatures, which is record high as well. More than half of the coral population in the northern section has perished, while the central and southern centers have been reported to be in better health.]]></description>
<guid>http://www.natureworldnews.com/articles/33132/20161130/great-barrier-reef-sees-worst-damage-record.htm</guid>
<link>http://www.natureworldnews.com/articles/33132/20161130/great-barrier-reef-sees-worst-damage-record.htm</link>
<media:content url="http://images.natureworldnews.com/data/images/full/37433/great-barrier-reef-sees-its-worst-damage-on-record.jpg" />
<media:title type="html"><![CDATA[Great Barrier Reef Sees Its Worst Damage on Record]]></media:title>
<media:text type="html"><![CDATA[Corals in the Great Barrier reef are in danger.
]]></media:text>
<category>
<name><![CDATA[News]]></name>
</category>
<pubDate>Wed, 30 Nov 2016 09:54:00 EST</pubDate>
</item>
请注意<media:content>
标签 - 这是图像的 URL 所在的位置。
我的代码为每个 RSS 条目抛出以下内容!有人可以解释一下#text
我在下面看到的值?有人可以帮我编写如何提取图像 URL 并将其放在 setImage
中的代码吗?方法?
12-01 01:58:36.278 27776-27823/com.example01 E/NODE NAME: media:content
12-01 01:58:36.278 27776-27823/com.example01 W/System.err: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String org.w3c.dom.Node.getNodeValue()' on a null object reference
12-01 01:58:36.278 27776-27823/com.example01 W/System.err: at com.climatenews07.parser.DOMParser.parseXml(DOMParser.java:74)
12-01 01:58:36.278 27776-27823/com.example01 W/System.err: at com.climatenews07.SplashActivity$AsyncLoadXMLFeed.doInBackground(SplashActivity.java:103)
12-01 01:58:36.278 27776-27823/com.example01 W/System.err: at com.climatenews07.SplashActivity$AsyncLoadXMLFeed.doInBackground(SplashActivity.java:97)
12-01 01:58:36.278 27776-27823/com.example01 W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:304)
12-01 01:58:36.278 27776-27823/com.example01 W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-01 01:58:36.278 27776-27823/com.example01 W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
12-01 01:58:36.278 27776-27823/com.example01 W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
12-01 01:58:36.278 27776-27823/com.example01 W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
12-01 01:58:36.278 27776-27823/com.example01 W/System.err: at java.lang.Thread.run(Thread.java:761)
12-01 01:58:36.278 27776-27823/com.example01 E/NODE NAME: #text
12-01 01:58:36.279 27776-27823/com.example01 W/System.err: java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String org.w3c.dom.Node.getNodeValue()' on a null object reference
12-01 01:58:36.279 27776-27823/com.example01 W/System.err: at com.climatenews07.parser.DOMParser.parseXml(DOMParser.java:74)
12-01 01:58:36.279 27776-27823/com.example01 W/System.err: at com.climatenews07.SplashActivity$AsyncLoadXMLFeed.doInBackground(SplashActivity.java:103)
12-01 01:58:36.279 27776-27823/com.example01 W/System.err: at com.climatenews07.SplashActivity$AsyncLoadXMLFeed.doInBackground(SplashActivity.java:97)
12-01 01:58:36.279 27776-27823/com.example01 W/System.err: at android.os.AsyncTask$2.call(AsyncTask.java:304)
12-01 01:58:36.279 27776-27823/com.example01 W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-01 01:58:36.279 27776-27823/com.example01 W/System.err: at android.os.AsyncTask$SerialExecutor$1.run(AsyncTask.java:243)
12-01 01:58:36.279 27776-27823/com.example01 W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
12-01 01:58:36.279 27776-27823/com.example01 W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
12-01 01:58:36.279 27776-27823/com.example01 W/System.err: at java.lang.Thread.run(Thread.java:761)
12-01 01:58:36.279 27776-27823/com.example01 E/NODE NAME: media:title
正因为如此,我也得到了以下异常;
12-01 01:58:36.500 27776-27927/com.example01 E/Image URL: http:
12-01 01:58:36.500 27776-27927/com.example01 W/System.err: java.net.UnknownHostException: Invalid host: http:
12-01 01:58:36.500 27776-27927/com.example01 W/System.err: at com.android.okhttp.HttpUrl.getChecked(HttpUrl.java:670)
12-01 01:58:36.500 27776-27927/com.example01 W/System.err: at com.android.okhttp.OkHttpClient$1.getHttpUrlChecked(OkHttpClient.java:165)
12-01 01:58:36.500 27776-27927/com.example01 W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.newHttpEngine(HttpURLConnectionImpl.java:345)
12-01 01:58:36.500 27776-27927/com.example01 W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.initHttpEngine(HttpURLConnectionImpl.java:331)
12-01 01:58:36.500 27776-27927/com.example01 W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getResponse(HttpURLConnectionImpl.java:398)
12-01 01:58:36.500 27776-27927/com.example01 W/System.err: at com.android.okhttp.internal.huc.HttpURLConnectionImpl.getInputStream(HttpURLConnectionImpl.java:243)
12-01 01:58:36.500 27776-27927/com.example01 W/System.err: at com.climatenews07.image.ImageLoader.getBitmap(ImageLoader.java:74)
12-01 01:58:36.500 27776-27927/com.example01 W/System.err: at com.climatenews07.image.ImageLoader.access$000(ImageLoader.java:27)
12-01 01:58:36.500 27776-27927/com.example01 W/System.err: at com.climatenews07.image.ImageLoader$PhotosLoader.run(ImageLoader.java:148)
12-01 01:58:36.500 27776-27927/com.example01 W/System.err: at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:428)
12-01 01:58:36.500 27776-27927/com.example01 W/System.err: at java.util.concurrent.FutureTask.run(FutureTask.java:237)
12-01 01:58:36.500 27776-27927/com.example01 W/System.err: at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1133)
12-01 01:58:36.501 27776-27927/com.example01 W/System.err: at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:607)
12-01 01:58:36.501 27776-27927/com.example01 W/System.err: at java.lang.Thread.run(Thread.java:761)
最佳答案
can someone help me code how to extract the image URL
提取 media:content
元素的 url
属性的值:
import org.w3c.dom.Element;
…
if ("media:content".equals(nodeName)) {
Element contentElement = (Element) thisNode;
if (contentElement.hasAttribute("url")) {
String u = contentElement.getAttribute("url");
}
}
该 fragment 转换 Node thisNode
到 Element这样就可以使用 getAttribute(…)
方法来获取 url
属性的值。
My code is throwing the following for every single RSS entry!
问题中的代码正在执行以下操作:
theString = nchild.item(j).getFirstChild().getNodeValue();
…例如,当 nchild.item(j)
是这样时:
<media:content url="http://images.natureworldnews.com/data/images/full/37450/earth-ocean.jpg" />
因此,在这种情况下,代码会在没有子元素的 media:content
元素上调用 .getFirstChild()
,从而返回 null
。然后代码调用 .getNodeValue()
,这会导致 java.lang.NullPointerException: Attempt to invoke interface method 'java.lang.String org.w3c.dom.Node .getNodeValue()' 出现空对象引用
错误。
代码的目的似乎是获取 url
属性的值。但属性不是子属性,因此 .getFirstChild()
将无法获取 url
属性。应该使用 .getAttribute(…)
来代替。
Can someone explain the #text value I see below
每个 item
元素不仅包含子元素,还包含文本节点——因为元素之间有空间。 .getChildNodes()
返回文本节点以及元素节点。
跳过文本节点的一种方法是在 for
循环的代码中添加如下内容:
if ("#text".equals(nodeName)) {
continue;
}
关于java - 如何调整我的 DOMParser 以从 Java (Android) 上的 RSS 解析 "media:content"?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40904640/
我一直在使用 AJAX 从我正在创建的网络服务中解析 JSON 数组时遇到问题。我的前端是一个简单的 ajax 和 jquery 组合,用于显示从我正在创建的网络服务返回的结果。 尽管知道我的数据库查
很难说出这里要问什么。这个问题模棱两可、含糊不清、不完整、过于宽泛或夸夸其谈,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开,visit the help center . 关闭 1
我在尝试运行 Android 应用程序时遇到问题并收到以下错误 java.lang.NoClassDefFoundError: com.parse.Parse 当我尝试运行该应用时。 最佳答案 在这
有什么办法可以防止etree在解析HTML内容时解析HTML实体吗? html = etree.HTML('&') html.find('.//body').text 这给了我 '&' 但我想
我有一个有点疯狂的例子,但对于那些 JavaScript 函数作用域专家来说,它看起来是一个很好的练习: (function (global) { // our module number one
关闭。此题需要details or clarity 。目前不接受答案。 想要改进这个问题吗?通过 editing this post 添加详细信息并澄清问题. 已关闭 8 年前。 Improve th
我需要编写一个脚本来获取链接并解析链接页面的 HTML 以提取标题和其他一些数据,例如可能是简短的描述,就像您链接到 Facebook 上的内容一样。 当用户向站点添加链接时将调用它,因此在客户端启动
在 VS Code 中本地开发时,包解析为 C:/Users//AppData/Local/Microsoft/TypeScript/3.5/node_modules/@types//index而不是
我在将 json 从 php 解析为 javascript 时遇到问题 这是我的示例代码: //function MethodAjax = function (wsFile, param) {
我在将 json 从 php 解析为 javascript 时遇到问题 这是我的示例代码: //function MethodAjax = function (wsFile, param) {
我被赋予了将一种语言“翻译”成另一种语言的工作。对于使用正则表达式的简单逐行方法来说,源代码过于灵活(复杂)。我在哪里可以了解更多关于词法分析和解析器的信息? 最佳答案 如果你想对这个主题产生“情绪化
您好,我在解析此文本时遇到问题 { { { {[system1];1;1;0.612509325}; {[system2];1;
我正在为 adobe after effects 在 extendscript 中编写一些代码,最终变成了 javascript。 我有一个数组,我想只搜索单词“assemble”并返回整个 jc3_
我有这段代码: $(document).ready(function() { // }); 问题:FB_RequireFeatures block 外部的代码先于其内部的代码执行。因此 who
背景: netcore项目中有些服务是在通过中间件来通信的,比如orleans组件。它里面服务和客户端会指定网关和端口,我们只需要开放客户端给外界,服务端关闭端口。相当于去掉host,这样省掉了些
1.首先贴上我试验成功的代码 复制代码 代码如下: protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec)
什么是 XML? XML 指可扩展标记语言(eXtensible Markup Language),标准通用标记语言的子集,是一种用于标记电子文件使其具有结构性的标记语言。 你可以通过本站学习 X
【PHP代码】 复制代码 代码如下: $stmt = mssql_init('P__Global_Test', $conn) or die("initialize sto
在SQL查询分析器执行以下代码就可以了。 复制代码代码如下: declare @t varchar(255),@c varchar(255) declare table_cursor curs
前言 最近练习了一些前端算法题,现在做个总结,以下题目都是个人写法,并不是标准答案,如有错误欢迎指出,有对某道题有新的想法的友友也可以在评论区发表想法,互相学习🤭 题目 题目一: 二维数组中的
我是一名优秀的程序员,十分优秀!