问题描述
我不能够解析我的XML here.It返回项目而已。
我AndroidActivity不能正常显示,因为它是非常大的,这就是为什么我只表示这是负责解析的部分。
我的XML是这样的:
< MyResource>
<项目>首先< /项目>
<项目>第二个< /项目>
< / MyResource>
我的ActivityClass方式:
公共类ShowItems延伸活动{
ListView的LV;
ListAdapter适配器;
静态最后弦乐KEY_RESOURCE =MyResource; //父节点
静态最后弦乐KEY_ITEM =项目;
ArrayList的< HashMap的<字符串,字符串>> mylist中=新的ArrayList< HashMap的<字符串,字符串>>();
的String []从= {KEY_ITEM};
INT []到= {R.id.mylist_item};
@覆盖
保护无效的onCreate(包savedInstanceState){
// TODO自动生成方法存根
super.onCreate(savedInstanceState);
的setContentView(R.layout.showitems);
LV =(ListView控件)findViewById(R.id.lv_items);
parseXML();
适配器=新SimpleAdapter(这一点,mylist中,R.layout.list_item,从,到);
lv.setAdapter(适配器);
}
私人无效parseXML(){
// TODO自动生成方法存根
XMLParser的分析器=新XMLParser的();
最终的字符串URL =http://10.0.2.2:8080/MySite/xml;
XML字符串= parser.getXmlFromUrl(URL);
文档DOC = parser.getDomElement(XML);
NodeList的NL = doc.getElementsByTagName(KEY_RESOURCE);
的for(int i = 0; I< nl.getLength();我++){
HashMap的<字符串,字符串>图=新的HashMap<字符串,字符串>();
元素e =(元)nl.item(我);
map.put(KEY_ITEM,parser.getValue(即KEY_ITEM));
mylist.add(图) }
}
我的XML解析器类:
进口java.io.IOException异常;
进口java.io.StringReader中;
进口java.io.UnsupportedEncodingException;
进口javax.xml.parsers.DocumentBuilder中;
进口javax.xml.parsers.DocumentBuilderFactory中;
进口javax.xml.parsers.ParserConfigurationException;
进口org.apache.http.HttpEntity;
进口org.apache.http.Htt presponse;
进口org.apache.http.client.ClientProtocolException;
进口org.apache.http.client.HttpClient;
进口org.apache.http.client.methods.HttpGet;
进口org.apache.http.impl.client.DefaultHttpClient;
进口org.apache.http.util.EntityUtils;
进口org.w3c.dom.Document中;
进口org.w3c.dom.Element中;
进口org.w3c.dom.Node中;
进口org.w3c.dom.NodeList;
进口org.xml.sax.InputSource中;
进口org.xml.sax.SAXException;
进口android.util.Log;
公共XMLParser类{
公共字符串getXmlFromUrl(字符串URL){
XML字符串= NULL;
尝试 {
HttpClient的HttpClient的=新DefaultHttpClient();
HTTPGET HTTPGET =新HTTPGET(URL);
HTT presponse HTT presponse = httpClient.execute(HTTPGET);
HttpEntity httpEntity = HTT presponse.getEntity();
XML = EntityUtils.toString(httpEntity);
}赶上(UnsupportedEncodingException E){
e.printStackTrace();
}赶上(ClientProtocolException E){
e.printStackTrace();
}赶上(IOException异常E){
e.printStackTrace();
}
返回XML;
}
公开文件getDomElement(XML字符串){
文档DOC = NULL;
DocumentBuilderFactory的DBF = DocumentBuilderFactory.newInstance();
尝试 {
DocumentBuilder的DB = dbf.newDocumentBuilder();
InputSource的是=新的InputSource();
is.setCharacterStream(新StringReader(XML));
DOC = db.parse(是);
}赶上(的ParserConfigurationException E){
Log.e(错误,e.getMessage());
返回null;
}赶上(的SAXException E){
Log.e(错误,e.getMessage());
返回null;
}赶上(IOException异常E){
Log.e(错误,e.getMessage());
返回null;
}
//返回DOM
返回文档;
}
公共字符串的getValue(元素项,字符串str){
节点列表N = item.getElementsByTagName(STR);
返回this.getElementValue(n.item(0));
}
公共最后弦乐getElementValue(节点ELEM){
子节点;
如果(ELEM!= NULL){
如果(elem.hasChildNodes()){
对于(子= elem.getFirstChild();!孩子= NULL;孩子= child.getNextSibling()){
如果(child.getNodeType()== Node.TEXT_NODE){
返回child.getNodeValue();
}
}
}
}
返回 ;
}
}
我不能够解析我的XML here.What是这里的问题?
它返回项目而已。
什么我需要做的,我ActivityClass特别是在这部分的code:
XML字符串= parser.getXmlFromUrl(URL);
文档DOC = parser.getDomElement(XML);
NodeList的NL = doc.getElementsByTagName(KEY_RESOURCE);
的for(int i = 0; I< nl.getLength();我++){
HashMap的<字符串,字符串>图=新的HashMap<字符串,字符串>();
元素e =(元)nl.item(我);
map.put(KEY_ITEM,parser.getValue(即KEY_ITEM));
mylist.add(图) }
您的getValue()
方法获取 MyResource
元,从那里,你需要获得在 MyResource
所有项目,并做 getElementValue()
。例如:code是:
公开地图的getValue(元素项,字符串str){
节点列表N = item.getElementsByTagName(STR);
的for(int i = 0; I< n.getLength();我++){
的System.out.println(getElementValue(n.item(我)));
}
//这里将其存储在列表/图并返回字符串列表/图代替
返回目录/ MapHere;
}
I am not able to parse my XML here.It returns "Item" only.
My AndroidActivity cannot be shown as it is very big that's why i have only shown the part which is responsible for parsing.
My XML Looks like this :
<MyResource>
<Item>First</Item>
<Item>Second</Item>
</MyResource>
My ActivityClass method:
public class ShowItems extends Activity{
ListView lv;
ListAdapter adapter;
static final String KEY_RESOURCE = "MyResource"; // parent node
static final String KEY_ITEM = "Item";
ArrayList<HashMap<String, String>> mylist = new ArrayList<HashMap<String, String>>();
String[] from={KEY_ITEM };
int[] to={R.id.mylist_item};
@Override
protected void onCreate(Bundle savedInstanceState) {
// TODO Auto-generated method stub
super.onCreate(savedInstanceState);
setContentView(R.layout.showitems);
lv=(ListView) findViewById(R.id.lv_items);
parseXML();
adapter = new SimpleAdapter(this, mylist,R.layout.list_item,from , to);
lv.setAdapter(adapter);
}
private void parseXML() {
// TODO Auto-generated method stub
XMLParser parser = new XMLParser();
final String URL="http://10.0.2.2:8080/MySite/xml";
String xml = parser.getXmlFromUrl(URL);
Document doc = parser.getDomElement(xml);
NodeList nl = doc.getElementsByTagName(KEY_RESOURCE);
for (int i = 0; i < nl.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
map.put(KEY_ITEM, parser.getValue(e, KEY_ITEM));
mylist.add(map); }
}
My XML Parser Class:
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.HttpClient;
import org.apache.http.client.methods.HttpGet;
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 {
public String getXmlFromUrl(String url) {
String xml = null;
try {
HttpClient httpClient = new DefaultHttpClient();
HttpGet httpGet = new HttpGet(url);
HttpResponse httpResponse = httpClient.execute(httpGet);
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;
}
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 DOM
return doc;
}
public String getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
return this.getElementValue(n.item(0));
}
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 "";
}
}
I am not able to parse my XML here.What is the problem here ?
It returns "Item" only.
What do i need to do in my ActivityClass specially in this part of the code :
String xml = parser.getXmlFromUrl(URL);
Document doc = parser.getDomElement(xml);
NodeList nl = doc.getElementsByTagName(KEY_RESOURCE);
for (int i = 0; i < nl.getLength(); i++) {
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
map.put(KEY_ITEM, parser.getValue(e, KEY_ITEM));
mylist.add(map); }
Your getValue()
method gets MyResource
element, from there, you need to get all Items under MyResource
and do getElementValue()
. Example code is:
public Map getValue(Element item, String str) {
NodeList n = item.getElementsByTagName(str);
for (int i = 0; i < n.getLength(); i++) {
System.out.println(getElementValue(n.item(i)));
}
//Here store it in list/map and return list/map instead of String
return list/MapHere;
}
这篇关于如何解析Android中的XML DOM解析相同的名称标签的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!