android都有哪些xml解析器,Android- XML解析器不是从XML返回全部价值

The Return of Toastmasters

http://www.younginnovations.com.np/blogs/anjan/2012/06/return-toastmasters

<p>As the title implies, it was the <strong>return of the Toastmasters</strong> at YoungInnovations on Thursday, May 24, 2012. I said &quot;return&quot; because Toastmasters saw a long gap of more than 3 months. Why the delay? Well, we shifted to a new office building and it took us some time to adjust properly into the new place. However, we're all glad that we continued with the event even after a long gap. And rightfully so, the MC for the day, Bimal Maharjan, announced the theme for the meeting: Return.</p>

<p><a href="http://www.younginnovations.com.np/blogs/anjan/2012/06/return-toastmasters" target="_blank">read more</a></p>

http://www.younginnovations.com.np/blogs/anjan/2012/06/return-toastmasters#comments

Toastmasters

Mon, 04 Jun 2012 07:28:33 +0000

anjan

151 at http://www.younginnovations.com.np

和我的应用程序代码值低于 News.java

import java.util.ArrayList;

import java.util.HashMap;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.NodeList;

import android.app.ListActivity;

import android.content.Intent;

import android.os.Bundle;

import android.util.Log;

import android.view.View;

import android.widget.AdapterView;

import android.widget.AdapterView.OnItemClickListener;

import android.widget.ListAdapter;

import android.widget.ListView;

import android.widget.SimpleAdapter;

import android.widget.TextView;

public class News extends ListActivity {

static final String URL = "http://www.younginnovations.com.np/blog/feed";

static final String KEY_ITEM = "item";

static final String KEY_TITLE = "title";

static final String KEY_DESC = "description";

static final String KEY_DATE = "pubDate";

@Override

protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);

setContentView(R.layout.news);

ArrayList> menuItems = new ArrayList>();

XMLParser parser = new XMLParser();

String xml = parser.getXmlFromUrl(URL);

Document doc = parser.getDomElement(xml);

NodeList nl = doc.getElementsByTagName(KEY_ITEM);

for (int i = 0; i < nl.getLength(); i++) {

HashMap map = new HashMap();

Element e = (Element) nl.item(i);

Log.e("Element", e.toString());

System.out.println(e);

map.put(KEY_TITLE, parser.getValue(e, KEY_TITLE));

map.put(KEY_DATE, parser.getValue(e, KEY_DATE));

map.put(KEY_DESC, parser.getValue(e, KEY_DESC));

menuItems.add(map);

}

ListAdapter adapter = new SimpleAdapter(this, menuItems,

R.layout.news_list_item,

new String[] { KEY_TITLE, KEY_DESC, KEY_DATE }, new int[] {

R.id.name, R.id.desciption, R.id.cost });

setListAdapter(adapter);

}

}

和XMLParse.java

import java.io.IOException;

import java.io.StringReader;

import java.io.UnsupportedEncodingException;

import javax.xml.parsers.DocumentBuilder;

import javax.xml.parsers.DocumentBuilderFactory;

import javax.xml.parsers.ParserConfigurationException;

import org.apache.http.HttpEntity;

import org.apache.http.HttpResponse;

import org.apache.http.client.ClientProtocolException;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.impl.client.DefaultHttpClient;

import org.apache.http.util.EntityUtils;

import org.w3c.dom.Document;

import org.w3c.dom.Element;

import org.w3c.dom.Node;

import org.w3c.dom.NodeList;

import org.xml.sax.InputSource;

import org.xml.sax.SAXException;

import android.util.Log;

public class XMLParser {

// constructor

public XMLParser() {

}

/**

* Getting XML from URL making HTTP request

* @param url string

* */

public String getXmlFromUrl(String url) {

String xml = null;

try {

// defaultHttpClient

DefaultHttpClient httpClient = new DefaultHttpClient();

HttpPost httpPost = new HttpPost(url);

HttpResponse httpResponse = httpClient.execute(httpPost);

HttpEntity httpEntity = httpResponse.getEntity();

xml = EntityUtils.toString(httpEntity);

} catch (UnsupportedEncodingException e) {

e.printStackTrace();

} catch (ClientProtocolException e) {

e.printStackTrace();

} catch (IOException e) {

e.printStackTrace();

}

// return XML

return xml;

}

/**

* Getting XML DOM element

* @param XML string

* */

public Document getDomElement(String xml){

Document doc = null;

DocumentBuilderFactory dbf = DocumentBuilderFactory.newInstance();

try {

DocumentBuilder db = dbf.newDocumentBuilder();

InputSource is = new InputSource();

is.setCharacterStream(new StringReader(xml));

doc = db.parse(is);

} catch (ParserConfigurationException e) {

Log.e("Error: ", e.getMessage());

return null;

} catch (SAXException e) {

Log.e("Error: ", e.getMessage());

return null;

} catch (IOException e) {

Log.e("Error: ", e.getMessage());

return null;

}

return doc;

}

/** Getting node value

* @param elem element

*/

public final String getElementValue(Node elem) {

Node child;

if(elem != null){

if (elem.hasChildNodes()){

for(child = elem.getFirstChild(); child != null; child = child.getNextSibling()){

if(child.getNodeType() == Node.TEXT_NODE){

return child.getNodeValue();

}

}

}

}

return "";

}

/**

* Getting node value

* @param Element node

* @param key string

* */

public String getValue(Element item, String str) {

NodeList n = item.getElementsByTagName(str);

return this.getElementValue(n.item(0));

}

}

但同时获得标签的价值..有HTML标签。因此,“

提前等待帮助, 感谢

+0

更改为JSON代表已下载的XML内容并将其用于数据操作。 - 检查链接 http://stackoverflow.com/questions/7724263/how-to-convert-xml-to-json-in-java –

2012-08-13 11:30:27

+0

需要在XML本身工作..不能在转换过程.. –

2012-08-13 11:36:33

+0

如果您在标记中收到HTML内容作为响应,则应在服务器端完成修复,将其全部转换为格式正确的数据,或者在任何WebView中加载整个描述内容。 –

2012-08-13 11:43:58

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值