[SalesForce] XML parsing using XMLstreamreader

I am trying to parse through an XML which looks something like

       <e:UDFs>
            <e:UDF name="Comments_20for_20Approvers">Cloning req 379023 - bypassing approvals</e:UDF>
            <e:UDF name="Delivery_20Model">
                <e:UDSElement>
                    <e:Description>
                        <e:value locale="en">Platinum</e:value>
                    </e:Description>
                </e:UDSElement>
            </e:UDF>
            <e:UDF name="Incentive_20Plan_20Code">
                <e:UDSElement>
                    <e:Description>
                        <e:value locale="en">RRP</e:value>
                    </e:Description>
                </e:UDSElement>
            </e:UDF>
            <e:UDF name="Job_Code">ICO050</e:UDF>
            <e:UDF name="Justification">
                <e:UDSElement>
                    <e:Description>
                        <e:value locale="en">Budgeted New</e:value>
                    </e:Description>
                </e:UDSElement>
            </e:UDF>
            <e:UDF name="Range_20Maximum"/>
            <e:UDF name="Target">1000</e:UDF>
            <e:UDF name="Underutilization_20Status">
                <e:UDSElement>
                    <e:Description>
                        <e:value locale="en">Underutilized for Minorities</e:value>
                    </e:Description>
                </e:UDSElement>
            </e:UDF>
        </e:UDFs>

As u can see there are several tags with tag named 'UDF' with different attributes like "Comments_20for_20Approvers" , "Delivery_20Model" and so on.

I want to parse through this XML and store values of these tags in different variables.
For example value of in a variable called comments, in a variable called incentive.

I want to use XMLstramreader. Following is my XMLstream

  Xmlstreamreader reader = new Xmlstreamreader(xmlBody);

Following is my code to go through the 'reader'

 while(reader.hasNext())
      {
        if (<check of a particular attribute>)
            {
             //do something

        }
      reader.next();
    } 

Not sure how to look for attribute value in the IF statement above.

Any help is greatly appreciated.

Best Answer

As per the Apex documentation here, you use the getAttributeLocalName method to determine this, but only on start element or attribute element events. Adapting the sample given in the documentation you can see this method in action below.

while(reader.hasNext()) {
    if (reader.getEventType() == XmlTag.START_ELEMENT) {
        if ('UDF' == reader.getLocalName()) {
            System.debug(reader.getAttributeValue(null, 'name'));
        }
    }
    reader.next();
} 

Text Handling. If you want to retrieve the test content of an element, you need to call getText() but only hasTest() returns true, e.g. when getEventType() == XmlTag.Characters. The documentation states that hasText() returns true and thus getText() works only when...

Returns true if the current event has text, false otherwise The following XML events have text: characters, entity reference, comment and space.