0

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>
developer
  • 107
  • 1
  • 4
  • 10
  • Did you try e.getAttribute("AttribName") ? – Fildor Apr 13 '16 at 11:31
  • i have error typo when i right e.getAttribute("AttribName") this is get value method public String getValue(Element item, String str) { NodeList n = item.getElementsByTagName(str); return this.getElementValue(n.item(0)); } – developer Apr 13 '16 at 11:46
  • You do not want the _Value_, you want the _Attribute_ ... – Fildor Apr 13 '16 at 11:48
  • yes i want to loop on the attributes of an element " roomtype" and display in list-view see this link for my question and my code of all what i want to do [link](http://stackoverflow.com/questions/36573011/xml-dom-parser-then-display-the-result-with-list-view/36575640?noredirect=1#comment60753964_36575640) – developer Apr 13 '16 at 11:59
  • You need to call getAttributes on the element then. – Fildor Apr 13 '16 at 12:03
  • display it in listview i have more than one room in xml file and description and id of the room i want to display them in list-view – developer Apr 13 '16 at 12:05
  • First things first. Get the data, then think about how to display. There are plenty ( and some of them even good ) tutorials about how to display data in a listview. So as soon as you have the data you want, you'll figure out the rest rather easily. – Fildor Apr 13 '16 at 12:08
  • i have the xml file data already i got it i want to get an element of them and display it at this line i have all of elements and data of xml file NodeList nl = doc.getElementsByTagName("*"); – developer Apr 13 '16 at 12:10
  • i am beginner with android please help me – developer Apr 13 '16 at 12:16
  • http://developer.android.com/guide/topics/ui/layout/listview.html The Developer Site is a good source, most often. – Fildor Apr 13 '16 at 12:28
  • [link](http://stackoverflow.com/questions/4138754/getting-an-attribute-value-in-xml-element) i want to do like this example – developer Apr 13 '16 at 12:29

1 Answers1

-1

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")
Community
  • 1
  • 1
ya man
  • 443
  • 1
  • 10
  • 15
  • 1
    I lightheadedly assumed it was only a snippet. But if it is the complete file, this would explain a lot :) – Fildor Apr 13 '16 at 11:53
  • yep, thanks @Fildor for the edit ... I realized that the closing tags are missing too. – ya man Apr 13 '16 at 12:00