5

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.

1
  • 1
    It might be worth better qualifying your question to be something like 'XML parsing attributes using XMLstreamreader' – Andrew Fawcett Jun 5 '13 at 1:04
5

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.

3
  • Thanks @Andrew Fawcett, this was really helpful. I am able to get to the desired tags. Now I have to pull text values from these tags. Is getText() best way to do it? This function will throw an exception as the Tag is a START_ELEMENT. – Yash Mehta Jun 5 '13 at 18:38
  • 1
    Thats correct, the documentation states when its possible to call getText(), see the hasText() method. I've updated my answer to reflect this additional query. Hope this helps! :) – Andrew Fawcett Jun 5 '13 at 18:50
  • Your very welcome, if this has answered your question don't forget to accept it. Thanks! – Andrew Fawcett Jun 5 '13 at 22:07

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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