Xml解析android

时间:2014-08-08 07:20:26

标签: android xml parsexml

我有xml,我想解析这个xml。

<?xml version="1.0" encoding="utf-8"?>
<arrays xmlns:android="http://schemas.android.com/apk/res/android">
    <array-nodes name="nodes_parter">
        <node coords="141,30" entry="0" >
           <neighbor coords="260,30" entry="0" />
           <neighbor coords="260,70" entry="0" />
           <neighbor coords="141,90" entry="0" />
           <neighbor coords="201,70" entry="0" />
           <neighbor coords="115,30" entry="1" />
        </node>
        <node coords="141,90" entry="0" >
           <neighbor coords="141,30" entry="0" />
           <neighbor coords="141,150" entry="0" />
           <neighbor coords="115,92" entry="1" />
        </node>
         <node coords="141,150" entry="0" >
           <neighbor coords="141,90" entry="0" />
           <neighbor coords="115,154" entry="0" />
           <neighbor coords="141,210" entry="0" />
           <neighbor coords="115,92" entry="1" />
        </node>
</array-nodes>
</arrays>

解析我用过这个方法: resId - xml文件夹中的id xml mapName =&#34; nodes_parter&#34;

public void setAreaResource(int resId, String mapName, Context ctx) 
    {
        boolean loading = false;
        Node node  = null;
        try 
        {
            XmlResourceParser xpp = ctx.getResources().getXml(resId);
            int eventType = xpp.getEventType();

            while (eventType != XmlPullParser.END_DOCUMENT) 
            {
                if(eventType == XmlPullParser.START_DOCUMENT) 
                {

                }
                else if(eventType == XmlPullParser.START_TAG) 
                {
                    String tag = xpp.getName();
                    //Log.d("Xml_parse", tag);
                    if (tag.equalsIgnoreCase("array-nodes")) 
                    {
                        String mapname = xpp.getAttributeValue(null, "name");

                        if (mapname !=null) 
                        {
                            if (mapname.equalsIgnoreCase(mapName)) 
                            {
                                loading=true;
                            }
                        }
                    }

                    if (loading) 
                    {
                        if(tag.equalsIgnoreCase("node"))
                        {
                            String coords = xpp.getAttributeValue(null, "coords");
                            String entry = xpp.getAttributeValue(null, "entry");
                            if(coords != null && entry!=null) 
                            {
                                node = new Node(coords,entry);
                                Log.d("Xml_parse",coords);
                            }
                        }
                        else if(tag.equalsIgnoreCase("neighbor"))
                        {
                            String coordsNode = xpp.getAttributeValue(null, "coords");
                            String entryNode = xpp.getAttributeValue(null, "entry");

                            if(coordsNode != null && entryNode!=null) 
                            {
                                Node neighbor = new Node(coordsNode,entryNode);
                                node.addNeighbor(neighbor);
                            }

                            Log.d("Xml_parse",coordsNode);
                        }
                    }
                } 
                else  if(eventType == XmlPullParser.END_TAG) 
                {
                    String tag = xpp.getName();

                    if (tag.equalsIgnoreCase("node")) 
                    {
                        map.add(node);
                    }

                    if (tag.equalsIgnoreCase("array-nodes")) 
                    {
                        loading = false;
                    }
                }               
                eventType = xpp.next();
            }
        } 
        catch (XmlPullParserException xppe) 
        {
            throw new RuntimeException("ImageMap::setAreaResource() - XmlPullParserException exception");
        } 
        catch (IOException ioe) 
        {
            throw new RuntimeException("ImageMap::setAreaResource() - XmlPullParserException exception");
        }
    }

我不知道,这种方法是否适合解析。您对此方法有何看法?

1 个答案:

答案 0 :(得分:0)

您可以使用此库来解析xml文件。 http://www.w3.org/2003/01/dom2-javadoc/org/w3c/dom/Document.html