1

嗨,我对 webserices 和 xml 解析非常陌生。我使用http://www.mysamplecode.com/2011/11/android-parse-xml-file-example-using.html这个链接作为我的参考。但我认为在这个链接中,xml 是一个简单的。如何解析像这样的复杂 xml。任何人都可以建议我一种方法来解析这个。我想从每个 MenuEntry 中读取 <A>、<B>、<C>、<D> 标签的值

        <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>

这是我用于解析 xml 的 xmlHandler 类

    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);
            }

        }

    }

这是我的菜单 java 类

    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;
        }
    }

我得到重复和空值。我知道我的编码可能是错误的,任何人都可以帮助我回复或任何有用的链接来解析这种复杂的xml(对我来说它很复杂:))

4

2 回答 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();
        }

将意味着当您遇到诸如外部<MenuEntry> ... <MenuEntries> ...<MenuEntry>挂起之类的序列时会被覆盖。一种快速而肮脏(未经测试!)的解决方案是:itemMenuEntry

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);
        }
    }
}

这应该维护一堆挂起的Menu结构。

于 2012-08-21T06:44:21.023 回答
1

http://simple.sourceforge.net/ 帮助我解析了这个 xml

于 2012-11-15T08:02:50.397 回答