Sunil George Jacob
Sunil George Jacob

Reputation: 100

Getting child nodes from an XML

In my application I have an XML file and I want to parse the XML file and extract data from the XML tags. Here is my XML file.

  <?xml version='1.0' encoding='utf-8'?>
    <OrdersEnquiryResponse Message="" >

       <TableDetails Outlet="S2" Date="01/01/15" Time="12:19:47" TableNo=" 1" Covers="2" Waiter="DD " KOTs=" 4981"/>

                <Items>
                    <Item Code="IBMCF" Name="BRANDY-MC.NO.1-750ML" Qty=" 1.000" Instructions="" Status="F"/>
                    <Item Code="IBMHF" Name="BRANDY-MANSION HOUSE-750ML" Qty=" 1.000" Instructions="" Status="F"/>
                    <Item Code="IBCRF" Name="BRANDY-CAESAR-750ML" Qty=" 1.000" Instructions="" Status="P"/>
                    <Item Code="IBLVF" Name="BRANDY�LOUIS VERNANT�750ML" Qty=" 1.000" Instructions="" Status="P"/>
               </Items>


       <TableDetails Outlet="S2" Date="01/01/15" Time="12:19:47" TableNo=" 4" Covers="2" Waiter="DD " KOTs=" 4982"/>

              <Items>
                    <Item Code="IBMRF" Name="BRANDY�MORPHEUS-XO-PREMIUM-750ML" Qty=" 1.000" Instructions="" Status="F"/>
                    <Item Code="IBMCF" Name="BRANDY-MC.NO.1-750ML" Qty=" 1.000" Instructions="" Status="F"/>
                    <Item Code="IBCRF" Name="BRANDY-CAESAR-750ML" Qty=" 1.000" Instructions="" Status="P"/>
                    <Item Code="IBLVF" Name="BRANDY�LOUIS VERNANT�750ML" Qty=" 1.000" Instructions="" Status="P"/>
             </Items>

     </OrdersEnquiryResponse>

I have used DOM parser to parse the above XML file.

        Document doc = parser.getDomElement(xml);

        NodeList nlll = doc.getElementsByTagName("TableDetails");

            int category_len = nlll.getLength();// length of Table Details
            Log.d("LENGTH 0", category_len + "");

            for (int i = 0; i < nlll.getLength(); i++) {
                Node nNode = nlll.item(i);
                if (nNode.getNodeType() == Node.ELEMENT_NODE) {
                    Element eElement = (Element) nNode;

                    outlet = eElement.getAttribute(KEY_OUTLET);
                    order_date = eElement.getAttribute(KEY_DATE);
                    order_time = eElement.getAttribute(KEY_TIME);
                    tbno = eElement.getAttribute(KEY_TNO);
                    waiter = eElement.getAttribute(KEY_WAITOR);
                    kotNo = eElement.getAttribute(KEY_KOT);
                    no_of_coverss = eElement.getAttribute(KEY_COVER);
                    System.out.println(order_date);
                    System.out.println(order_time);


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

                    int category_len1 = nl.getLength();// length of Item
                    Log.d("LENGTH 1", category_len1 + "");
                    column1 = new String[category_len1];
                    column2 = new String[category_len1];
                    column3 = new String[category_len1];
                    column4 = new String[category_len1];
                    column5 = new String[category_len1];
                    column6 = new String[category_len1];


                    for (int j = 0; j < nl.getLength(); j++) {
                        Node nNode1 = nl.item(i);
                        if (nNode1.getNodeType() == Node.ELEMENT_NODE) {
                            Element eElement1 = (Element) nNode1;

                            column4[i] = (i + 01) + "";
                            column5[i] = eElement1.getAttribute(KEY_CODE);
                            column1[i] = eElement1.getAttribute(KEY_NAME);
                            column2[i] = eElement1.getAttribute(KEY_QUANTITY);
                            column3[i] = eElement1.getAttribute(KEY_INSTRUCTION);
                            column6[i] = eElement1.getAttribute(KEY_STATUS);

                        }                       
                    }

                }
            }

Whenever I get the Item tag NodeList nl = doc.getElementsByTagName("Item"); I'm getting all the <Item> tag. I only need the first four <Item> tag when I get the first <TableDetails> tag. How can this be made possible. Thanks in advance.

Upvotes: 0

Views: 1129

Answers (1)

Jithesh V
Jithesh V

Reputation: 36

Please change the code:

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

to

 NodeList nl = eElement .getElementsByTagName("Item");

now you will get only the corresponding child values.

Because when you use doc variable it will search for entire file.

While eElement search for only the nodes under its category/Main Node("TableDetails").

And also please change the variable name to 'j' inside the second for loop. you are using 'i' instead of 'j'.

Check it out.

Upvotes: 1

Related Questions