- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
首先,我是 Android 和 Java 的新手。我一直在尝试将“简单”的 Rss 阅读器添加到我的应用程序中。我想要它做的就是解析来自特定站点的 RSS 提要并获取标题和发布日期并将这些项目放入 ListView 中,当单击列表项目时它将打开浏览器以显示包含该文章的网页。够简单吧?很遗憾地说,这让我困惑了几天。
我已经阅读了一些教程/示例,我正在尝试修改以适合我的目的的代码如下所示:
主要 Activity
public class News extends Activity {
private final String MY_DEBUG_TAG = "Some NEWS";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
/* Create a new TextView to display the parsingresult later. */
TextView tv = new TextView(this);
try {
/* Create a URL we want to load some xml-data from. */
URL url = new URL("http://www.some.com/feed/");
/* Get a SAXParser from the SAXPArserFactory. */
SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser();
/* Get the XMLReader of the SAXParser we created. */
XMLReader xr = sp.getXMLReader();
/* Create a new ContentHandler and apply it to the XML-Reader*/
ExampleHandler myExampleHandler = new ExampleHandler();
xr.setContentHandler(myExampleHandler);
/* Parse the xml-data from our URL. */
xr.parse(new InputSource(url.openStream()));
/* Parsing has finished. */
/* Our ExampleHandler now provides the parsed data to us. */
ParsedExampleDataSet parsedExampleDataSet =
myExampleHandler.getParsedData();
/* Set the result to be displayed in our GUI. */
tv.setText(parsedExampleDataSet.toTitle());
} catch (Exception e) {
/* Display any Error to the GUI. */
tv.setText("Error: " + e.getMessage());
Log.e(MY_DEBUG_TAG, "NewsQueryError", e);
}
/* Display the TextView. */
this.setContentView(tv);
}
}
示例处理器
public class ExampleHandler extends DefaultHandler{
// ===========================================================
// Fields
// ===========================================================
private boolean in_entry = false;
private boolean in_id = false;
private boolean in_title = false;
private boolean in_updated = false;
private boolean in_summary = false;
private boolean in_link = false;
private ParsedExampleDataSet myParsedExampleDataSet = new ParsedExampleDataSet();
// ===========================================================
// Getter & Setter
// ===========================================================
public ParsedExampleDataSet getParsedData() {
return this.myParsedExampleDataSet;
}
// ===========================================================
// Methods
// ===========================================================
@Override
public void startDocument() throws SAXException {
this.myParsedExampleDataSet = new ParsedExampleDataSet();
}
@Override
public void endDocument() throws SAXException {
// Nothing to do
}
/** Gets be called on opening tags like:
* <tag>
* Can provide attribute(s), when xml was like:
* <tag attribute="attributeValue">*/
@Override
public void startElement(String namespaceURI, String localName,
String qName, Attributes atts) throws SAXException {
if (localName.equals("entry")) {
this.in_entry = true;
}else if (localName.equals("id")) {
this.in_id = true;
}else if (localName.equals("title")) {
this.in_title = true;
}else if (localName.equals("updated")){
this.in_updated = true;
}else if (localName.equals("summary")) {
this.in_summary = true;
}else if (localName.equals("link")) {
this.in_link = true;
}
}
/** Gets be called on closing tags like:
* </tag> */
@Override
public void endElement(String namespaceURI, String localName, String qName)
throws SAXException {
if (localName.equals("entry")) {
this.in_entry = false;
}else if (localName.equals("id")) {
this.in_id = false;
}else if (localName.equals("title")) {
this.in_title = false;
}else if (localName.equals("updated")) {
this.in_updated = false;
}else if (localName.equals("summary")) {
this.in_summary = false;
}else if (localName.equals("link")) {
this.in_link = false;
}
}
/** Gets be called on the following structure:
* <tag>characters</tag> */
public void characters(char ch[], int start, int length) {
if(this.in_title){
myParsedExampleDataSet.setextractedTitle(new String(ch, start, length));
}
}
}
解析示例数据集
public class ParsedExampleDataSet {
private String extractedId = null;
private String extractedTitle = null;
private String extractedUpdated = null;
private String extractedSummary = null;
private String extractedImage = null;
private int extractedInt = 0;
public String getextractedId() {
return extractedId;
}
public void setextractedId(String extractedId) {
this.extractedId = extractedId;
}
public String getextractedTitle() {
return extractedTitle;
}
public void setextractedTitle(String extractedTitle) {
this.extractedTitle = extractedTitle;
}
public String getextractedUpdated() {
return extractedUpdated;
}
public void setextractedUpdated(String extractedUpdated) {
this.extractedUpdated = extractedUpdated;
}
public String getextractedSummary() {
return extractedSummary;
}
public void setextractedSummary(String extractedSummary) {
this.extractedSummary = extractedSummary;
}
public String toId(){
return this.extractedId;
}
public String toTitle(){
return this.extractedTitle;
}
public String toUpdated(){
return this.extractedUpdated;
}
public String toSummary(){
return this.extractedSummary;
}
}
当然,这只会返回提要中的最后一个条目。它正在正确解析,因为我可以获得要单独显示的每个元素。我对如何实现 listveiw 一无所知。
提前致谢
最佳答案
让我们一步一步来,希望我们能实现您想要做的事情。我已经包含了您需要了解的 API 文档的相关链接。
DocumentBuilder builder = factory.newDocumentBuilder();
Document dom = builder.parse(this.getInputStream());
Element root = dom.getDocumentElement();
<p></p>
<pre><code>import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
public class RSSAdapter extends BaseAdapter {
Element rootElement;
public RSSAdapter(Element rootElement) {
this.rootElement = rootElement;
}
public int getCount() {
// get the count of children of the root element
return 0;
}
public Object getItem(int position) {
// get the child identified by the position,
// this is the important part where you
// want to get the right child representing the RSS data that you want to get
return null;
}
public long getItemId(int position) {
return position;
}
public View getView(int position, View convertView, ViewGroup parent) {
// implement your view here....
return null;
}
}
</code></pre>
<p></p>
这应该足以让您实现您想要的。如果步骤不清楚,我会进一步澄清。希望这会有所帮助。
关于Android:Sax 解析为 ListView ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7136593/
我一直在尝试为 SAX 解析器设置 UI 线程和处理程序。这是我没有实现 UI 线程和处理程序的解析器: public class AndroidXMLReader extends ListActiv
我正在尝试读取一个大型 XML 文档,并且我想以 block 的形式读取它,而不是 XmlDocument 将整个文件读入内存的方式。我知道我可以使用 XmlTextReader 来做到这一点,但我想
我正在尝试将 11384 个 XML 文件解析到一个 SQLite 数据库中。其中之一: ]> 1 2 我正在使用 SAX 解析器: pub
我需要用 Perl 解析 XML 文件。文件的一部分单独存储,并作为系统实体插入。不过,这个问题很常见。但是我无法获得任何信息来解决它。 ]> &externalContent; 当使
我正在尝试从 xsd 解析 HL7 消息定义。我将模式定义分成两个文件。第一个文件包含实际的消息定义,第二个文件包含消息中的段定义。 我正在尝试调整示例代码以从此处解析 XML https://gis
我正在使用 SAX 解析 MathML 表达式(尽管它是 MathML 的事实可能并不完全相关)。输入字符串示例为 λ 为了让 SAX 解
我正在尝试使用 Java 和 SAX 为 Android 设备解析 XML 文件。我从互联网上获取,在解析它时,我得到一个 ExpatException :字符“é”的格式不正确(无效标记)。有没有办
我正在尝试使用java中的sax读取xml文件。我只获得 endElement 的输出,但无法找出 startElement 出了什么问题。 这是我的处理程序: public class XMLHan
我正在使用 SAX 解析器来解析具有父级及其子级标记的 XML,如下所示:
我正在尝试从 RSS 提要中提取数据。 RSS 链接 - http://www.thehindu.com/sport/?service=rss ? 这是我的默认处理程序的字符方法。 public vo
我尝试执行下面的代码,但我的 SAX 解析器没有调用 startElement 方法。 下面是我的代码: package getTableStructure; import java.util.Lis
我正在满足一项要求,即我需要拆分大型 XML 并进一步处理。 这是 XML 示例,它可以变成单行。 yongjin 这是我的代码: import java.util.Arrays; import ja
我正在尝试将标签的内容放入我的 java Sax 解析器中的变量中。但是,Characters 方法仅返回 Char 数组。有没有办法将 Char 数组转换为 Int??? public void c
我有下面的代码.. System.setProperty("http.proxyHost","176.6.129.25") ; System.setProp
如何使用 SAX 显示树中最大深度的节点名称。该算法很适合我理解这个概念.. 例如,我应该如何使用 startelement、endelement、startdocument、enddocument
有没有可行的方法使用默认的处理程序类来查找对应的XML标签?例如... 1 1 我想使用 startElement() 和 endElement() 方
我想使用 SAX 解析器从 xml 文件中解析一些数据。我的xml如下: Pies & past Fruits 为了解析这些数据,我扩展了 DefaultHandler。 解析后的输出
我正在构建一个创建 XML 的流程(从各种来源并出于我事先不知道的各种目的),并且我希望将生成的 XML 直接注入(inject)到标准 XML 处理中,例如 SAX、StAX和 DOM。我已经完成了
我有一个由 MS Excel 创建的 XML 文件,其中包含如下元素: 22. Department"GS "NAES "ABCDEF"
我在使用 java sax 解析器打开 stackoverflow 帖子 XML 转储时遇到问题。它识别每个元素的结尾,但似乎跳过了 startElement 方法。我使用示例代码: try {
我是一名优秀的程序员,十分优秀!