解析strings.xml文件Android

时间:2013-12-12 10:31:46

标签: android xml string

我需要将这个strings.xml文件解析为我的Android应用程序:

<resources>
<string name="app">Cofely OpenVPN</string>
<string name="copyright_cofely">
Cofely-GDFSuez version <remco.ijntema@cofely-gdfsuez.nl>
</string>
<string name="copyright_blinktgui">
Copyright 2012.2013 Arne Schwabe <arne@rfc2549.org>
</string>
<string name="version_info_cofely">
This version is made by Remco IJntema @ Cofely-GDFSuez
</string>
<string name="copyright_guicode">
This Cofely-OpenVPN app is based on the following source code: http://code.google.com/p/ics-openvpn/
</string>
<string name="copyright_others">
This program uses the following components; see the source code for full details on the licenses
</string>
<string name="tracker">
Issue tracker:
<a href="http://sirius/tracker">tracker</a>
</string>
<string name="JSPWiki">
Wiki:
<a href="http://sirius:8080/JSPWiki">wiki</a>
</string>
<string name="svn">
Source code SVN:
<a href="http://sirius/svn">svn</a>
</string>
<string name="qlikview">
Qlikview:
<a href="http://qlikview/qlikview">QlikView</a>
</string>
<string name="opevpn_copyright">
Copyright © 2002.2010 OpenVPN Technologies, Inc. <sales@openvpn.net>\n "OpenVPN" is a trademark of OpenVPN Technologies, Inc.\n
</string>
<string name="defaultserver">openvpn.uni-paderborn.de</string>
<string name="defaultport">1194</string>
<string name="copyright_file_dialog">File Dialog based on work by Alexander Ponomarev</string>
<string name="lzo_copyright">
Copyright © 1996 . 2011 Markus Franz Xaver Johannes Oberhumer
</string>
<string name="copyright_openssl">
This product includes software developed by the OpenSSL Project for use in the OpenSSL Toolkit\n Copyright © 1998-2008 The OpenSSL Project. All rights reserved.\n\n This product includes cryptographic software written by Eric Young (eay@cryptsoft.com)\n Copyright © 1995-1998 Eric Young (eay@cryptsoft.com) All rights reserved.
</string>
<string name="openvpn">OpenVPN</string>
<string name="file_dialog">File Dialog</string>
<string name="lzo">LZO</string>
<string name="openssl">OpenSSL</string>
<string name="unknown_state">Unknown state</string>
<string name="permission_description">Allows another app to control OpenVPN</string>
<string name="bouncy_castle">Bouncy Castle Crypto APIs</string>
<string name="copyright_bouncycastle">
Copyright © 2000.2012 The Legion Of The Bouncy Castle (http://www.bouncycastle.org)
</string>
</resources>

我尝试使用我在网站上使用的代码:

 public List parse(InputStream in) throws XmlPullParserException, IOException {
        try {
            XmlPullParser parser = Xml.newPullParser();
            parser.setFeature(XmlPullParser.FEATURE_PROCESS_NAMESPACES, false);
            parser.setInput(in, null);
            parser.nextTag();
            return readFeed(parser);
        } finally {
            in.close();
        }
    }

private List<String> readFeed(XmlPullParser parser) throws XmlPullParserException, IOException {
    List<String> entries = new ArrayList<String>();

    parser.require(XmlPullParser.START_TAG, ns, "resources");
    while (parser.next() != XmlPullParser.END_TAG) {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            continue;
        }
        String name = parser.getName();
        entries.add(readEntry(parser));
    }  
    return entries;
}

private String readEntry(XmlPullParser parser) throws XmlPullParserException, IOException {
    parser.require(XmlPullParser.START_TAG, ns, "resources");
    String title = null;
    int i = 0;
    while (parser.next() != XmlPullParser.END_TAG) {
        if (parser.getEventType() != XmlPullParser.START_TAG) {
            continue;
        }
        String name = parser.getName();
        if (name.equals("string")){
            i++;
            title = readTitle(parser);
            System.out.println("This is my string: " + title);
        }
    }
    return title;
}

// Processes title tags in the feed.
private String readTitle(XmlPullParser parser) throws IOException, XmlPullParserException {
    parser.require(XmlPullParser.START_TAG, ns, "string");
    String title = readText(parser);
    parser.require(XmlPullParser.END_TAG, ns, "string");
    return title;
}

private String readText(XmlPullParser parser) throws IOException, XmlPullParserException {
    String result = "";
    if (parser.next() == XmlPullParser.TEXT) {
        result = parser.getText();
        parser.nextTag();
    }
    return result;
}

}

但我得到一个例外:

12-12 11:27:01.871: W/System.err(11902): org.xmlpull.v1.XmlPullParserException: expected: START_TAG {null}resources (position:START_TAG <string name='app'>@3:21 in java.io.InputStreamReader@41b54148) 
12-12 11:27:01.871: W/System.err(11902):    at org.kxml2.io.KXmlParser.require(KXmlParser.java:2046)

谁能告诉我发生了什么事?感谢

0 个答案:

没有答案