我计划通过将xml解析为文本来将博客转换为Android应用程序。我已经完成了java代码。现在,我想将它显示在一个活动列表中。我希望列表视图这个java代码的xml代码。你能帮我解决这个问题吗?
你可以从这里找到xml源代码:http://vestro-blogger-theme.blogspot.com/feeds/posts/default
public class MainActivity extends AppCompatActivity {
String title[];
String content[];
String contentHTML[];
String thumbimage[];
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// You have to change your blogspot address here
// for example : http://YOURBLOGADDRESS.blogspot.com/feeds/posts/default
new StartParsing().execute("http://vestro-blogger-theme.blogspot.com/feeds/posts/default");
}
private class StartParsing extends AsyncTask<String, Void, Void> {
@Override
protected Void doInBackground(String... params) {
String getURL = params[0];
try {
URL url = new URL(getURL);
DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();
DocumentBuilder db = dbf.newDocumentBuilder();
Document doc = db.parse(new InputSource(url.openStream()));
doc.getDocumentElement().normalize();
NodeList nodeList = doc.getElementsByTagName("title");
title = new String[nodeList.getLength()-1];
content = new String[nodeList.getLength()-1];
contentHTML = new String[nodeList.getLength()-1];
thumbimage = new String[nodeList.getLength()-1];
for (int i = 0; i < nodeList.getLength()-1; i++) {
Node node = nodeList.item(i);
title[i] = new String();
content[i] = new String();
contentHTML[i] = new String();
thumbimage[i] = new String();
//Get blogspot title
NodeList titleList = doc.getElementsByTagName("title");
Element titleElement = (Element)titleList.item(i+1);
titleList = titleElement.getChildNodes();
title[i] = ((Node)titleList.item(0)).getNodeValue();
//Get blog spot content as HTML
NodeList contentHTMLList = doc.getElementsByTagName("content");
Element contentHTMLElement = (Element)contentHTMLList.item(i+1);
contentHTMLList = contentHTMLElement.getChildNodes();
contentHTML[i] = ((Node)contentHTMLList.item(0)).getNodeValue();
// Make plain text from HTML
content[i] = String.valueOf(Html.fromHtml(Html.fromHtml(((Node)contentHTMLList.item(0)).getNodeValue()).toString()));
// Get Post content thumbnail
int start = contentHTML[i].indexOf("src=\"") + 5;
int end = contentHTML[i].indexOf("\"",start);
thumbimage[i] = contentHTML[i].substring(start,end);
}
}catch(Exception e) {
Log.d("Log","XML parsing exception = "+e);
}
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
// Check the data using log
Log.d("Log","Title is");
for(int i=0; i<title.length; i++) {
Log.d("Log",title[i]);
}
Log.d("Log","content is");
for(int i=0; i<content.length; i++) {
Log.d("Log",content[i]);
}
Log.d("Log","thumbimage is");
for(int i=0; i<thumbimage.length; i++) {
Log.d("Log",thumbimage[i]);
}
Log.d("Log","contentHTML is");
for(int i=0; i<contentHTML.length; i++) {
Log.d("Log",contentHTML[i]);
}
}
}
}
答案 0 :(得分:0)
我非常惊讶您正在解析XML数据以将您的博客转换为应用。也许你可以实现它,但为什么首先要做这样的过程?
您只需使用BLOGGER API解析博客内容。
Blogger API v3允许客户端应用程序查看和更新 Blogger内容。您的客户端应用程序可以使用Blogger API v3 创建新博客帖子,编辑或删除现有帖子,以及查询 符合特定条件的帖子。
创建基于浏览器的应用程序和支持人员的移动应用程序 从任何地方创建和管理他们的帖子。
您可能非常擅长XML解析,它可能适合您,但是当简单的API调用可以获取结果时,为什么会使任务复杂化。我强烈建议其他人使用Blogger API而不是XML Parsing。