使用单个ID和多个值解析XML - Android

时间:2016-02-18 14:51:52

标签: java android xml

String var = "<?xml version="1.0" encoding="UTF-8"?>
<current func = "123" name="lllo" add="pol" ..... />";

获取此xml需要获取存储在此xml中的值

我尝试过xmlparser来获取valeus 如

    String func= 123;
    String name = lllo;
..........

怎么可能?需要帮助

更新:我试过这个变为null:

XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
XmlPullParser pullParser = factory.newPullParser();
InputStream is =
        new ByteArrayInputStream(newString.getBytes());
pullParser.setInput(is, "UTF-8");

String min = pullParser.getAttributeValue(null, "uid");
String max = pullParser.getAttributeValue(null, "name");
Toast.makeText(getApplicationContext(), ":" + min, Toast.LENGTH_LONG).show();
System.out.println(":" + min + "" + max);

1 个答案:

答案 0 :(得分:0)

当然,这是一个独立的代码片段,正如您所期望的那样:

public class SimpleXmlPullApp
{

    public static void main (String args[])
            throws XmlPullParserException, IOException
    {
        XmlPullParserFactory factory = XmlPullParserFactory.newInstance();
        factory.setNamespaceAware(true);
        XmlPullParser xpp = factory.newPullParser();

        xpp.setInput( new StringReader ( "<current func = \"123\" name=\"lllo\" add=\"pol\" />" ) );
        int eventType = xpp.getEventType();
        while (eventType != XmlPullParser.END_DOCUMENT) {
            if(eventType == XmlPullParser.START_TAG) {
                for(int i=0; i<xpp.getAttributeCount(); i++) {
                    System.out.println("Attribute name: " + xpp.getAttributeName(i) + " - Attribute value: " + xpp.getAttributeValue(i));
                }
            }
            eventType = xpp.next();
        }
    }
}

运行它将输出:

Attribute name: func - Attribute value: 123
Attribute name: name - Attribute value: lllo
Attribute name: add - Attribute value: pol