简体   繁体   中英

How to get attribute value from xml in android

I am currently parsing Xml bye Dom but I want to get the elements and the attributes of them I don't know how I get it , I can not get the elements name and values and attributes of each element also can help me please.

 private void parsexml(String xmlresult){

    ListView myList=(ListView)findViewById(android.R.id.list);

    menuItems = new ArrayList<HashMap<String, String>>();

    XmlParser parser = new XmlParser();
    Document doc = parser.getDomElement(xmlresult); // getting DOM element
    NodeList nl = doc.getElementsByTagName("*");



    // looping through all item nodes <item>
    for (int i = 0; i < nl.getLength(); i++) {

        Node nNode = nl.item(i);
        if (nNode.getNodeType() == Node.ELEMENT_NODE) {

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

            Element eElement = (Element) nNode;
            String tagname = eElement.getTagName();
            // creating new HashMap
            HashMap<String, String> map = new HashMap<String, String>();

            // 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));
            map.put(KEY_categroy, parser.getValue(e, KEY_categroy));
            map.put(KEY_order, parser.getValue(e, KEY_order));
            // 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,KEY_categroy,KEY_order }, new int[] {
            R.id.name, R.id.desciption, R.id.cost,R.id.categroy,R.id.order });

    setListAdapter(adapter);
    ((BaseAdapter)adapter).notifyDataSetChanged();
}

xml

 <AvailableProducts> <Roomtype IDRoom="1" Code="A1D" NormBed="1" Maxbed="2" Category="3" Order="4"><Description Name="Deluxe Double Room" Text="" IDLanguage="4"/> <Roomtype IDRoom="1" Code="A1D" NormBed="1" Maxbed="2" Category="3" Order="4"><Description Name="Deluxe Double Room" Text="" IDLanguage="4"/></AvailableProducts> 

Hej,

at first your xml does not look like a DOM tree. A DOM consists of a root element (for example 'AvailableProducts') holding several children. In order to parse through the children like your code suggests, you need to give the getElementsByTagName() method the tagname of your root element:

NodeList nl = doc.getElementsByTagName("AvailableProducts");

Have a look at this or that .

Otherwise consider using a XMLPullParser which a bit more complicated, but you're able to define exactly what you want.

I hope this will help you. Cheers.

Edit: xml looks better now. I updated the code in my answer. If the xml data is received correctly, you should be able to get the attributes as Fildor suggested with

e.getAttribute("AttribName")

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM