Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I want to get the values of the key reset_periods in an array using XmlPullParser.

My xml file:

<?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>

I tried like so:

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();

}

Then I do the following:

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

This calls the following method:

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;
}

But I am not getting the 2044-01-01 01:00:00 and 2044-01-01 05:00:00 in the string array.

share|improve this question

1 Answer 1

This is how this can be done:

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;
    }

Result:

2044-01-01 01:00:00,2044-01-01 05:00:00
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.