1

I've the following xml file inside the /mnt/sdcard. I want to get the version of the item type=provider from the following file. The file is big(1500 lines) which has other types also. This is simplified file. In the file I'm interested in this node:

<Item Type="Provider" Version="19.0.0.0"Checksum="EShHVeNtW1xTfEvLvATwqA==" FileSize="2746200" />

From this node I want to get the version i.e. 19.0.0.0.

Here is my xml file:

<Manifest guid="FD29E1EF-A5C4-4D19-ACC8-8C98C7E91B02" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" PackageType="Full" Scope="APPROVED">
   <Items>
        <Item id="fcxm-8ikj-olk-ffgcxh3" Checksum="EShHVeNtW1xTfEvLvATwqA==" value="f425921f-b6ef-4e58-8a14-fcbd0d7e50e9" />
        <Item Type="question" Version="19.0.0.0"Checksum="EShHVeNtW1xTfEvLvATwqA==" FileSize="2746200" />
        <Item Type="Provider" Version="19.0.0.0"Checksum="EShHVeNtW1xTfEvLvATwqA==" FileSize="2746200" />
    </Items>
</Manifest>

I searched on the internet, I got this which is iterating to all the nodes of item type. I dont want want to iterate.

How can I do this in Android using XmlPullParser?

1 Answer 1

0

Hi you can try this,

private void parseContent(XmlPullParser parser) 
    throws XmlPullParserException,IOException,Exception {
    int eventType;
    while((eventType=parser.next()) != XmlPullParser.END_TAG) {
        if (eventType == XmlPullParser.START_TAG) {
            Log.d(MY_DEBUG_TAG,"Parsing Attributes for ["+parser.getName()+"]");
            Map<String,String> attributes = getAttributes(parser);
        }
        else if(eventType==...);
        else {
            throw new Exception("Invalid tag at content parse");
        }
    }
}

private Map<String,String>  getAttributes(XmlPullParser parser) throws Exception {
    Map<String,String> attrs=null;
    int acount=parser.getAttributeCount();
    if(acount != -1) {
        Log.d(MY_DEBUG_TAG,"Attributes for ["+parser.getName()+"]");
        attrs = new HashMap<String,String>(acount);
        for(int x=0;x<acount;x++) {
            Log.d(MY_DEBUG_TAG,"\t["+parser.getAttributeName(x)+"]=" +
                    "["+parser.getAttributeValue(x)+"]");
            attrs.put(parser.getAttributeName(x), parser.getAttributeValue(x));
        }
    }
    else {
        throw new Exception("Required entity attributes missing");
    }
    return attrs;
}
Sign up to request clarification or add additional context in comments.

3 Comments

thanks for the quick response, can u please explain what is going on. I dont want to iterate all the items because my xml has 1500+ items of the name 'item`.
You can not get attribute without iterating. Because parser have to get tag firstly then can reach tags attribute.
maybe you think about reading xml line by line. And put it to temp file by sorting line which contains "provider". Then you can use this temp xml to parse

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.