使用Android Listview示例代码进行xml解析 [英] xml parse using Android Listview sample code
本文介绍了使用Android Listview示例代码进行xml解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
如何访问子类别标签内的标题标签
sample.xml文件
how to access the title tag which is inside subcategory tag
sample.xml file
<?xml version="1.0" encoding="UTF-8"?>
<rss>
<item>
<id>1</id>
<title>Guidliness</title>
<desc>On the Internet usually the resume are send via e-mail to respective companies, but it is still important that you carry a copy along with you during your preliminary interview.</desc>
</item>
<item>
<id>2</id>
<title>Why do you Need a Resume?</title>
<desc>A summary of your objectives,educational qualification,experience,skills relevant to the field of work you are going to enter.</desc>
</item>
<item>
<id>3</id>
<title>Basics of Writing a Resume</title>
<subcategory>
<title>Basic Standards</title>
<desc>Resume is what an employer looks at much before meeting you,it is the first interface you have with your employer.</desc>
</subcategory>
<subcategory>
<title>Stick of the Point</title>
<desc>Employers have a busy schedule, so don't expect them to read through a long resume.</desc>
</subcategory>
<subcategory>
<title>Words Count</title>
<desc>Use of language is extremely important; you need to sell yourself to an employer quickly and efficiently.</desc>
</subcategory>
<subcategory>
<title>Make the Most of your Experience</title>
<desc>Employers need to know what you have accomplished to have an idea of what you can do for them. Don't be vague.</desc>
</subcategory>
<subcategory>
<title>Honesty is a Good Policy</title>
<desc>Employers will feel more comfortable hiring you if they can verify your accomplishments.</desc>
</subcategory>
<subcategory>
<title>Double-Check for Mistakes</title>
<desc>Check your resume for correct grammar and spelling - evidence of good communication skills and attention to detail.</desc>
</subcategory>
</item>
<item>
<id>4</id>
<title>15 Golden Steps</title>
<desc>The thought of writing a resume scares almost anyone. It's difficult to know where to start or what to include.</desc>
</item>
<item>
<id>5</id>
<title>8 Red Flags</title>
<desc>A Resume is your mirror reflection to a recruiter or prospective employer.</desc>
</item>
</rss>
Android中的SampleActivity.Java文件
SampleActivity.Java file in Android
public class AndroidXMLParsingActivity extends ListActivity {
// All static variables
static final String URL = "http://10.0.2.2:8080/sample/xmlfiles/sample.xml";
// XML node keys
static final String RSS_ITEM = "item"; // parent node
static final String RSS_ID = "id";
static final String RSS_NAME = "title";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<hashmap><string,>> menuItems = new ArrayList<hashmap><string,>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(RSS_ITEM);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++)
{
// creating new HashMap
HashMap<string,> map = new HashMap<string,>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap RSS => value
map.put(RSS_ID, parser.getValue(e, RSS_ID));
map.put(RSS_NAME, parser.getValue(e, RSS_NAME));
// adding HashList to ArrayList
menuItems.add(map);
}
// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(this, menuItems,R.layout.flist_item,new String[] { RSS_NAME }, new int[]
{
R.id.name
});
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView {
// getting values from selected ListItem
String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(RSS_NAME, name);
Log.d("name", name);
startActivity(in);
}
});
}
}</item></hashmap></hashmap>
推荐答案
查看此链接
listview-in-android [ ^ ]
这篇关于使用Android Listview示例代码进行xml解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文