解析Android中使用SAX本地XML文件 [英] Parsing local XML file using Sax in Android

查看:144
本文介绍了解析Android中使用SAX本地XML文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

谁能告诉我如何分析存储在使用SAX系统中的本地XML文件,用一个例子code?也请告诉我,我在哪里可以找到这些信息。

Can anyone tell me how to parse a local XML file stored in the system using SAX, with an example code? Please also tell me where can I find information on that.

推荐答案

从XML读取你的应用程序,在你的资源文件夹中创建您的项目称为XML(小写)中的文件夹。存储XML在这个新创建的文件夹中。要从资源加载XML,

To read from XML in your app, create a folder in your res folder inside your project called "xml" (lower case). Store your xml in this newly created folder. To load the XML from your resources,

XmlResourceParser myxml = mContext.getResources().getXml(R.xml.MyXml);//MyXml.xml is name of our xml in newly created xml folder, mContext is the current context
// Alternatively use: XmlResourceParser myxml = getContext().getResources().getXml(R.xml.MyXml);

myxml.next();//Get next parse event
int eventType = myxml.getEventType(); //Get current xml event i.e., START_DOCUMENT etc.

您就可以开始处理节点,属性等,并包含在文本由套管的事件类型,处理一次呼叫myxml.next()来获取下一个事件,即

You can then start to process nodes, attributes etc and text contained within by casing the event type, once processed call myxml.next() to get the next event, i.e.,

 String NodeValue;
    while (eventType != XmlPullParser.END_DOCUMENT)  //Keep going until end of xml document
    {  
        if(eventType == XmlPullParser.START_DOCUMENT)   
        {     
            //Start of XML, can check this with myxml.getName() in Log, see if your xml has read successfully
        }    
        else if(eventType == XmlPullParser.START_TAG)   
        {     
            NodeValue = myxml.getName();//Start of a Node
            if (NodeValue.equalsIgnoreCase("FirstNodeNameType"))
            {
                    // use myxml.getAttributeValue(x); where x is the number
                    // of the attribute whose data you want to use for this node

            }

            if (NodeValue.equalsIgnoreCase("SecondNodeNameType"))
            {
                    // use myxml.getAttributeValue(x); where x is the number
                    // of the attribute whose data you want to use for this node

            } 
           //etc for each node name
       }   
        else if(eventType == XmlPullParser.END_TAG)   
        {     
            //End of document
        }    
        else if(eventType == XmlPullParser.TEXT)   
        {    
            //Any XML text
        }

        eventType = myxml.next(); //Get next event from xml parser
    }

这篇关于解析Android中使用SAX本地XML文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆