views:

39

answers:

1

My XML looks like:

<key>A</key>

How do I extract the string "A" from the XML with java in Eclipse? My Java code looks like:

    XmlResourceParser xmlSymptoms = getResources().getXml(R.xml.symptoms);
    while (eventType != XmlResourceParser.END_DOCUMENT) {
        if (eventType == XmlResourceParser.START_TAG) {
            // Get the name of the tag (eg scores or score)
            String strName = xmlSymptoms.getName();
            if (strName.equals("key")) {
                 keyName = xmlSymptoms.getAttributeName(0);
                 keyA = xmlSymptoms.getAttributeValue(0);
            }
         }
         eventType = xmlSymptoms.next();
    }

But I think AttributeName and AttributeValue are for items that are part of the key description, i.e. they would be used to obtain "item" and "first".

<key item="first">A</key>

What do I use to obtain the string between "<key>" and "</key>"?

+1  A: 

You need to watch for the XMLResourceParser.TEXT event in addition to the START_TAG event. START_TAG only is concerned with <key item="first">. TEXT is concerned with A. END_TAG </key> etc.

Once you've recieved a TEXT event, call xmlSymptoms.getText(). This would return "A" in your example.

CodeFusionMobile