0

Hi im very new to webserices and xml parsing.I use http://www.mysamplecode.com/2011/11/android-parse-xml-file-example-using.html this link as my reference. But i think in this link the xml is a simple one.How can i parse a complex xml like this one.Can any one suggest me a method to parse this. I want to read the values of < A >,< B >,< C >,< D > tags from each MenuEntry

        <Menu>
         <MenuEntries>

           <MenuEntry>
           <A>--AAAAA---</A>
           <B>--BBBB---</B>
           <MenuEntries/>
           <C>--CCCC---</C>
           <D>--DDDD---</D>
           </MenuEntry>

            <MenuEntry>
            <B>--BBBB---</B>
            <menuEntries/>
            <C>--CCCC---</C>
            <D>--DDDD---</D>
            </MenuEntry>

            <MenuEntry>
            <A>--AAAAA---</A>
            <B>--BBBB---</B>
             <MenuEntries>

                <MenuEntry>
                <A>--AAAAA---</A>
                <B>--BBBB---</B>
                <MenuEntries/>
                <C>--CCCC---</C>
                <D>--DDDD---</D>
                </MenuEntry>

                <MenuEntry>
                <A>--AAAAA---</A>
                <B>--BBBB---</B>
                <MenuEntries/>
                <C>--CCCC---</C>
                <D>--DDDD---</D>
                </MenuEntry>

             </MenuEntries>
           <C>--CCCC---</C>
           <D>--DDDD---</D>
             </MenuEntry>

        <MenuEntry>
        <B>--BBBB---</B>
        <MenuEntries/>
        <C>--CCCC---</C>
        <D>--DDDD---</D>
          </MenuEntry>

        <MenuEntry>
        <A>--AAAAA---</A>
        <B>--BBBB---</B>
        <MenuEntries>

            <MenuEntry>
            <A>--AAAAA---</A>
            <B>--BBBB---</B>
            <MenuEntries/>
            <C>--CCCC---</C>
            <D>--DDDD---</D>
            </MenuEntry>

        </MenuEntries>
        <C>--CCCC---</C>
        <D>--DDDD---</D>
          </MenuEntry>

        </MenuEntries>
        </Menu>

This is my xmlHandler class for parsing the xml

    public class ItemXMLHandler extends DefaultHandler {

        Boolean currentElement = false;
        String currentValue = "";
        Menu item = null;
         static ArrayList<Menu> menuList = new ArrayList<Menu>();

        public static ArrayList<Menu> getMenuListz() {

            Log.e("in getMenulist()", String.valueOf(menuList.size()));
            return menuList;
        }

        // Called when tag starts
        @Override
        public void startElement(String uri, String localName, String qName,
                Attributes attributes) throws SAXException {

            currentElement = true;
            currentValue = "";
            if (localName.equals("MenuEntry")) {
                //Log.e("menuENtry found ", "menuENtry found");
                item = new Menu();

            }

        }

        @Override
        public void endElement(String uri, String localName, String qName)
                throws SAXException {

            currentElement = false;

            /** set value */
            if (localName.equalsIgnoreCase("A")) {
                item.setA(currentValue);
            } else if (localName.equalsIgnoreCase("B")) {
                item.setB(Long.parseLong(currentValue));
            } else if (localName.equalsIgnoreCase("C")) {
                    item.setC(currentValue);
            } else if (localName.equalsIgnoreCase("menuEntry"))

                menuList.add(item);

        }

        // Called to get tag characters
        @Override
        public void characters(char[] ch, int start, int length)
                throws SAXException {

            if (currentElement) {
                currentValue = currentValue + new String(ch, start, length);
            }

        }

    }

This is my java class for Menu

    public Class Menu{
        private long B;
        private String A;
        private String C;

        public long getB() {
            return B;
        }
        public void setB(long B){
            this.B = B;
        }
        public String getA() {
            return A
        }
        public void setA(String A){
            this.A = A
        }
        public String getC() {
            return C
        }
        public void setC(String C) {
            this.C = C;
        }
    }

Im getting duplications and null values.I know my coding could be wrong can any one help me with a reply or any useful links for parsing this kind of complex xml ( for me its complex :) )

4
  • 1
    That XML is not even well-formed! Are you sure you are getting this as the input? Commented Aug 21, 2012 at 5:41
  • Actually its not the orginal xml i edited it to ask here.Not well formed means? Should i do anything to make it well formed?How a well formed xml look like?What can i do with the above xml? I know im asking too much questions.If u can explain(links) these plz post here.Anyways thanks for the fast reply.Lemme start googling :) Commented Aug 21, 2012 at 5:56
  • Give a link ok your XML web service. Commented Aug 21, 2012 at 6:00
  • coders-global.com/works/dev/menuserivicetemp.xml Commented Aug 21, 2012 at 6:14

2 Answers 2

1

Looking at the original XML file, the problem is that the sample code you have chosen assumes there is no nesting of the XML data, so the lines of code here:

        if (localName.equals("MenuEntry")) {
            //Log.e("menuENtry found ", "menuENtry found");
            item = new Menu();
        }

will mean that when you encounter as sequence like <MenuEntry> ... <MenuEntries> ...<MenuEntry> the pending item for the outer MenuEntry is overwritten. One quick and dirty (and untested!) solution is:

public class ItemXMLHandler extends DefaultHandler {

    Boolean currentElement = false;
    String currentValue = "";
    /*DELETE Menu item = null; */
    static ArrayList<Menu> menuList = new ArrayList<Menu>();

    /*NEW*/ Stack<Menu> items;

    public static ArrayList<Menu> getMenuListz() {

        Log.e("in getMenulist()", String.valueOf(menuList.size()));
        return menuList;
    }

    // Called when tag starts
    @Override
    public void startElement(String uri, String localName, String qName,
            Attributes attributes) throws SAXException {

        currentElement = true;
        currentValue = "";
        if (localName.equals("MenuEntry")) {
            //Log.e("menuENtry found ", "menuENtry found");
            /*UPDATE*/ items.push(new Menu());

        }

    }

    @Override
    public void endElement(String uri, String localName, String qName)
            throws SAXException {

        currentElement = false;
        /*NEW*/ Menu item = items.peek();
        /** set value */
        if (localName.equalsIgnoreCase("A")) {
            item.setA(currentValue);
        } else if (localName.equalsIgnoreCase("B")) {
            item.setB(Long.parseLong(currentValue));
        } else if (localName.equalsIgnoreCase("C")) {
                item.setC(currentValue);
        } else if (localName.equalsIgnoreCase("menuEntry"))
            /*NEW*/ items.pop();
            menuList.add(item);
    }

    // Called to get tag characters
    @Override
    public void characters(char[] ch, int start, int length)
            throws SAXException {

        if (currentElement) {
            currentValue = currentValue + new String(ch, start, length);
        }
    }
}

This should maintain a stack of pending Menu structures.

Sign up to request clarification or add additional context in comments.

4 Comments

Thanks for the reply.I will try it. I created this dummy xml from coders-global.com/works/dev/menuserivicetemp.xml Is it well formed?
This page says it's OK. Note that I had to cut and paste the XML into the form rather than using the URL verification.
By the way, I edited your original post as it was not well-formed, with some </MenuEntries> instead of <MenuEntries/>. See the revision history.
Thank you so much .Actually i didnt knew we could validate xml.Well i tested using the URL method and im getting "failure"
1

http://simple.sourceforge.net/ helped me in parsing this xml

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.