【问题标题】:Parsing xml file using XmlPullParser to get values in a string array使用 XmlPullParser 解析 xml 文件以获取字符串数组中的值
【发布时间】:2013-07-16 13:20:39
【问题描述】:

我想使用 XmlPullParser 在数组中获取键 reset_periods 的值。

我的 xml 文件:

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" 
    "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>main_reset</key>
    <integer>32</integer>

<key>mandatory_period</key>
    <integer>111</integer>
    <key>reset_periods</key>
    <array>
        <string>2044-01-01 01:00:00</string>
        <string>2044-01-01 05:00:00</string>
    </array>
    </dict>
</plist>

我试过这样:

int eventType = xpp.getEventType();
int i = 0;

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

    } else if (eventType == XmlPullParser.END_DOCUMENT) {

    } else if (eventType == XmlPullParser.START_TAG) {
        xmldoc += "<" + xpp.getName() + ">";

    } else if (eventType == XmlPullParser.END_TAG) {
        xmldoc += "</" + xpp.getName() + ">";

    } else if (eventType == XmlPullParser.TEXT) {

        xmldoc += xpp.getText();
    }
    eventType = xpp.next();

}

然后我执行以下操作:

mandatory_period = (xml.getValues("reset_periods"));

这会调用以下方法:

public ArrayList<String> getValues(String key) {
    int start = this.xmldoc.indexOf(key + "</string>");
    String xmldoc2 = this.xmldoc.substring(start);
    xmldoc2 = xmldoc2.substring(start + 6, end);
    String[] spl = xmldoc2.split("<string>");
    ArrayList<String> spl2 = new ArrayList<String>();
    for (int x = 1; x < spl.length; x++) {
        String[] spx = spl[x].split("</string>");
        spl2.add(spx[0]);
    }
    return spl2;
}

但我没有在字符串数组中得到2044-01-01 01:00:002044-01-01 05:00:00

【问题讨论】:

    标签: android xml-parsing xmlpullparser


    【解决方案1】:

    这是如何做到的:

    public String getValues(String key) {
    
            int start = this.xmldoc.indexOf(key + "</key><array>");
            String xmldoc2 = this.xmldoc.substring(start);
    
            int end = xmldoc2.lastIndexOf("</string");
    
            xmldoc2 = xmldoc2.substring(39, end);
    
            xmldoc2 = xmldoc2.replace("</string><string>", ",");
    
            return xmldoc2;
        }
    

    结果:

    2044-01-01 01:00:00,2044-01-01 05:00:00
    

    【讨论】:

      猜你喜欢
      • 2013-04-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-01-26
      • 1970-01-01
      • 2013-10-17
      • 2017-05-07
      相关资源
      最近更新 更多