W
wiesel25
Neues Mitglied
- 3
Hallo Leute,
ich zerbreche mir gerade den Kopf beim xml parsing.
Ich nutze den u.g Code der einwandfrei bei normalem XML-Dokument funtzt.
Mein Problem besteht daran dass wenn ich auf ein XMl Dokument zugreife das Attribute in einem Knoten besitzt , ich an diese Attribute nicht rankomme.
Mit diesem xml example funtzt der Code:
Sagen wir mal das XML Dokument sieht so aus und hat in dem Knoten "item" Attribute. Ich möchte aus dem Knoten "item" das Attribut "type" auslesen.
Kann mir jemand einen Tip geben wie ich das mit dem u.g. Code anstellen kann.
Gruss
wiesel25
ich zerbreche mir gerade den Kopf beim xml parsing.
Ich nutze den u.g Code der einwandfrei bei normalem XML-Dokument funtzt.
Mein Problem besteht daran dass wenn ich auf ein XMl Dokument zugreife das Attribute in einem Knoten besitzt , ich an diese Attribute nicht rankomme.
Mit diesem xml example funtzt der Code:
Code:
<?xml version="1.0" encoding="UTF-8"?>
<menu>
<item>
<id>1</id>
<name>Margherita</name>
<cost>155</cost>
<description>Single cheese topping</description>
</item>
<item>
<id>2</id>
<name>Double Cheese Margherita</name>
<cost>225</cost>
<description>Loaded with Extra Cheese</description>
</item>
<item>
<id>3</id>
<name>Fresh Veggie</name>
<cost>110</cost>
<description>Oninon and Crisp capsicum</description>
</item>
<item>
<id>4</id>
<name>Peppy Paneer</name>
<cost>155</cost>
<description>Paneer, Crisp capsicum and Red pepper</description>
</item>
<item>
<id>5</id>
<name>Mexican Green Wave</name>
<cost>445</cost>
<description>Onion, Crip capsicum, Tomato with mexican herb</description>
</item>
</menu>
Sagen wir mal das XML Dokument sieht so aus und hat in dem Knoten "item" Attribute. Ich möchte aus dem Knoten "item" das Attribut "type" auslesen.
Kann mir jemand einen Tip geben wie ich das mit dem u.g. Code anstellen kann.
Code:
<item type="Pizza">
<id>1</id>
<name>Margherita</name>
<cost>155</cost>
<description>Single cheese topping</description>
</item>
Code:
public class AndroidXMLParsingActivity extends ListActivity {
// All static variables
static final String URL = "http://api.androidhive.info/pizza/?format=xml";
// XML node keys
static final String KEY_ITEM = "item"; // parent node
static final String KEY_ID = "id";
static final String KEY_NAME = "name";
static final String KEY_COST = "cost";
static final String KEY_DESC = "description";
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
ArrayList<HashMap<String, String>> menuItems = new ArrayList<HashMap<String, String>>();
XMLParser parser = new XMLParser();
String xml = parser.getXmlFromUrl(URL); // getting XML
Document doc = parser.getDomElement(xml); // getting DOM element
NodeList nl = doc.getElementsByTagName(KEY_ITEM);
// looping through all item nodes <item>
for (int i = 0; i < nl.getLength(); i++) {
// creating new HashMap
HashMap<String, String> map = new HashMap<String, String>();
Element e = (Element) nl.item(i);
// adding each child node to HashMap key => value
map.put(KEY_ID, parser.getValue(e, KEY_ID));
map.put(KEY_NAME, parser.getValue(e, KEY_NAME));
map.put(KEY_COST, "Rs." + parser.getValue(e, KEY_COST));
map.put(KEY_DESC, parser.getValue(e, KEY_DESC));
// adding HashList to ArrayList
menuItems.add(map);
}
// Adding menuItems to ListView
ListAdapter adapter = new SimpleAdapter(this, menuItems,
R.layout.list_item,
new String[] { KEY_NAME, KEY_DESC, KEY_COST }, new int[] {
R.id.name, R.id.desciption, R.id.cost });
setListAdapter(adapter);
// selecting single ListView item
ListView lv = getListView();
// listening to single listitem click
lv.setOnItemClickListener(new OnItemClickListener() {
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// getting values from selected ListItem
String name = ((TextView) view.findViewById(R.id.name)).getText().toString();
String cost = ((TextView) view.findViewById(R.id.cost)).getText().toString();
String description = ((TextView) view.findViewById(R.id.desciption)).getText().toString();
// Starting new intent
Intent in = new Intent(getApplicationContext(), SingleMenuItemActivity.class);
in.putExtra(KEY_NAME, name);
in.putExtra(KEY_COST, cost);
in.putExtra(KEY_DESC, description);
startActivity(in);
}
});
}
}
Gruss
wiesel25