从xml文件中获取价值

时间:2011-04-15 05:11:57

标签: android android-2.2-froyo

我有一个xml文件

<?xml version="1.0" encoding="utf-8"?> 
<NewDataSet> <Password>abcd</Password> </NewDataSet>

如何从上面的xml文件中获取字符串“abcd”。 我对android平台很新,请帮帮我 提前谢谢

2 个答案:

答案 0 :(得分:1)

根据您的评论和问题,我建议您参考以下链接:

  1. http://www.anddev.org/parsing_xml_from_the_net_-_using_the_saxparser-t353.html
  2. http://www.ibm.com/developerworks/opensource/library/x-android/
  3. http://www.androidpeople.com/android-xml-parsing-tutorial-%E2%80%93-using-domparser
  4. 从第一个链接开始,您可以逐步进行操作,第二个链接包含所有解析技术的示例。

    我建议使用SAX(Simple API for XML)Parser。

答案 1 :(得分:0)

在我看来,通过一个例子更容易理解某些内容,甚至更多,如果我们知道我们希望从该示例中做什么的示例。

所以我会发布一个小样本,除了你需要的东西之外什么都不做:从那个xml文件中读取密码并以readPassword()方法返回它:

import java.io.StringReader;

import javax.xml.parsers.SAXParser;
import javax.xml.parsers.SAXParserFactory;

import org.xml.sax.Attributes;
import org.xml.sax.InputSource;
import org.xml.sax.SAXException;
import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler;

public class XmlSample
{
    private static final String xmlSource = 
        "<?xml version='1.0' encoding='utf-8'?>" +
        "<NewDataSet>" +
        "   <Password>abcd</Password>" +
        "</NewDataSet>";
    public final class MyXmlHandler  extends DefaultHandler
    {
        /**
         * the Password tag's value
         */
        private String  password;
        /**
         * for keeping track where the cursor is right now
         */
        private String currentNodeName;

        public MyXmlHandler()
        {
        }

        /**
         * It is called when starting to process a new element (tag)
         * At this point you change the currentNodeName member
         */
        @Override
        public void startElement(String namespaceURI, String localName, String qName, Attributes atts) throws SAXException
        {
            this.currentNodeName = localName;
        }

        /**
         * It is called when an element's processing has finished 
         *      ("</ _tag>" or "... />" is reached) 
         * Clear the currentNodeName member
         */
        @Override
        public void endElement(String namespaceURI, String localName, String qName) throws SAXException
        {
            this.currentNodeName = null;
        }

        /**
         * It is called when the currentNodeName tag's body is processed.
         * In the ch[] array are the character values of that element.
         */
        @Override
        public void characters(char ch[], int start, int length)
        {
            if (this.currentNodeName.equals("Password"))
                password = new String(ch, start, length);
        }

        public String getPassword()
        {
            return password;
        }
    }

    public String readPassword() throws Exception
    {
        //create an inputSource from the xml value;
        //when you get this xml from the server via http, you should use something like:
        //HttpEntity responseEntity = response.getEntity();
        //final InputSource input = new InputSource(responseEntity.getContent());
        final InputSource input = new InputSource(new StringReader(xmlSource));

        SAXParserFactory parserFactory = SAXParserFactory.newInstance();
        SAXParser parser = parserFactory.newSAXParser();
        XMLReader reader = parser.getXMLReader();

        MyXmlHandler myHandler = new MyXmlHandler();
        //attach your handler to the reader
        reader.setContentHandler(myHandler);

        //parse the input InputSource. It will fill your myHandler instance
        reader.parse(input);
        return myHandler.getPassword();
    }
}

代码本身非常小,我只是插入了一些注释以便更好地理解。 如果您需要更多帮助,请告诉我。