我创建了XML解析RSS阅读器,但它只读取最后3个节点(标题,链接和描述)。我想知道哪些行代码制作这个技巧(我的意思是读取最后的节点)。我想从这个rss site读取前三个节点,我应该做什么? 我是个新人。任何人都可以将答案作为代码发布吗?
public class HandleXML {
private String title = "title";
private String link = "link";
private String description = "description";
private String title1 = "title";
private String link1 = "link";
private String description1 = "description";
private String urlString = null;
private XmlPullParserFactory xmlFactoryObject;
public volatile boolean parsingComplete = true;
public HandleXML(String url){
this.urlString = url;
}
public String getTitle(){
return title;
}
public String getLink(){
return link;
}
public String getDescription(){
return description;
}
public void parseXMLAndStoreIt(XmlPullParser myParser) {
int event;
String text=null;
try {
event = myParser.getEventType();
while (event != XmlPullParser.END_DOCUMENT) {
String name=myParser.getName();
switch (event){
case XmlPullParser.START_TAG:
break;
case XmlPullParser.TEXT:
text = myParser.getText();
break;
case XmlPullParser.END_TAG:
if(name.equals("title")){
title = text;
}
else if(name.equals("link")){
link = text;
}
else if(name.equals("description")){
description = text;
}
else{
}
break;
}
event = myParser.next();
}
parsingComplete = false;
}
catch (Exception e) {
e.printStackTrace();
}
}
public void fetchXML(){
Thread thread = new Thread(new Runnable(){
@Override
public void run() {
try {
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
// Starts the query
conn.connect();
InputStream stream = conn.getInputStream();
xmlFactoryObject = XmlPullParserFactory.newInstance();
XmlPullParser myparser = xmlFactoryObject.newPullParser();
myparser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
myparser.setInput(stream, null);
parseXMLAndStoreIt(myparser);
stream.close();
}
catch (Exception e) {
}
}
});
thread.start();
}
}
答案 0 :(得分:0)
这部分代码负责阅读上面提到的元素。
if(name.equals("title")){
title = text;
}
else if(name.equals("link")){
link = text;
}
else if(name.equals("description")){
description = text;
}
else{
// Write similar conditions for the other tags that you want to parse.
}
答案 1 :(得分:0)
您正在使用while循环来处理文档。 在该循环中,您的代码将读取所有标题,链接和描述节点。 问题是它每次都会覆盖变量,因此你得到的是最后的标题,链接和描述。
如果您只想阅读第一个标题,链接和说明,您可以执行以下操作:
初始化变量如下:
private String title;
private String link;
private String description;
比:
if(name.equals("title") && title != null){
title = text;
}
else if(name.equals("link") && link != null){
link = text;
}
else if(name.equals("description") && description != null){
description = text;
}
编辑(试试这个,警告:我没有测试过它)
public class HandleXML {
public volatile boolean parsingComplete = true;
private String title;
private String link;
private String description;
private String urlString = null;
private XmlPullParserFactory xmlFactoryObject;
public HandleXML(String url) {
this.urlString = url;
}
public String getTitle() {
return title;
}
public String getLink() {
return link;
}
public String getDescription() {
return description;
}
public void parseXMLAndStoreIt(XmlPullParser myParser) {
int event;
String text = null;
try {
event = myParser.getEventType();
while (event != XmlPullParser.END_DOCUMENT) {
String name = myParser.getName();
switch (event) {
case XmlPullParser.START_TAG:
break;
case XmlPullParser.TEXT:
text = myParser.getText();
break;
case XmlPullParser.END_TAG:
if (name.equals("title") && title != null) {
title = text;
}
else if (name.equals("link") && link != null) {
link = text;
}
else if (name.equals("description") && description != null) {
description = text;
}
break;
}
event = myParser.next();
}
parsingComplete = false;
}
catch (Exception e) {
e.printStackTrace();
}
}
public void fetchXML() {
Thread thread = new Thread(new Runnable() {
@Override
public void run() {
try {
URL url = new URL(urlString);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setReadTimeout(10000 /* milliseconds */);
conn.setConnectTimeout(15000 /* milliseconds */);
conn.setRequestMethod("GET");
conn.setDoInput(true);
// Starts the query
conn.connect();
InputStream stream = conn.getInputStream();
xmlFactoryObject = XmlPullParserFactory.newInstance();
XmlPullParser myparser = xmlFactoryObject.newPullParser();
myparser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
myparser.setInput(stream, null);
parseXMLAndStoreIt(myparser);
stream.close();
}
catch (Exception e) {
// TODO: 27/07/2017 handle exception
}
}
});
thread.start();
}
}