1

Hello fellow Salesforce devs!

I hope this question haven't been asked before, but i havent found any articles explaining the same issue so here it goes.

Im trying to parse an XML string that we get from an external system. While using DOM.document I can parse through all of the elements just fine. However the attribute values of the elements are lost while traversing the string?

Example XML:

<AttributeStatement>
  <Attribute Name="http://schemas.microsoft.com/identity/claims/tenantid">
    <AttributeValue>Tenant ID</AttributeValue>
  </Attribute>
  <Attribute Name="http://schemas.microsoft.com/identity/claims/objectidentifier">
    <AttributeValue>ObjectID</AttributeValue>
  </Attribute>
  <Attribute Name="http://schemas.microsoft.com/ws/2008/06/identity/claims/groups">
    <AttributeValue>88f2b054-e2c7-4432-9847-f24ed82b0f6d</AttributeValue>
    <AttributeValue>87fdeb5c-2bad-4076-b407-97ce35eca54f</AttributeValue>
  </Attribute>
</AttributeStatement>

The specific element children i need is the attribute element which has the attribute name = http://schemas.microsoft.com/ws/2008/06/identity/claims/groups

However, when i've traversed the tree to this object the attribute Name can no longer be found, which makes it hard to get the text from the correct element. by using an if statement that checks the Name attribute of the element.

The code i use to parse the XML looks like this:

    Dom.document doc = new Dom.Document();

    doc.load(xmlString);

    dom.XmlNode xroot = doc.getrootelement();

    xnAttributeList = xroot.getChildElement('Assertion', NS).getChildElement('AttributeStatement',NS).getChildElements();


    List<DOM.XmlNode> xnAttributeValList = new List<DOM.XmlNode>();
    for(Dom.XmlNode xn : xnAttributeList){
         xnAttributeValList = xn.getChildElements();
            for(Dom.XmlNode xnv : xnAttributeValList){
                System.debug(xnv.getText());
            }
    }

What i get when i debug the XMLNode of an Attribute:

AttributeXMLNode[ELEMENT,Attribute,urn:oasis:names:tc:SAML:2.0:assertion,[common.apex.api.dom.XmlNode$Attribute@3d9184a5],null,[XMLNode[ELEMENT,AttributeValue,urn:oasis:names:tc:SAML:2.0:assertion,null,null,[XMLNode[TEXT,null,null,null,null,null,Test salesforce,]],null,]],null,]

The only thing i still can see here is the Namespace and the actual child element text. Where am I doing this wrong?

I'm pretty new to working with XML so all help that might put me in the right direction is greatly appreciated!

1
  • So what I'm doing now is just pulling the correct node from my Attribute list using the list index since i know that the XML structure most likely always will be the same. This works fine, but does seem like a not so elegant hack.
    – Cnork
    Oct 20, 2019 at 10:48

1 Answer 1

1

You can get any attribute of a Dom.xmlNode using the getAttribute(key, keyNamespace) method. None of your attibutes contain a namespace so just use and empty string or null for the second parameter. For example here is a version of your code that should achieve what you want:

   Dom.document doc = new Dom.Document();

    doc.load(xmlString);

    dom.XmlNode xroot = doc.getrootelement();

    List<dom.XmlNode> xnAttributeList = xroot.getChildElements();


    List<DOM.XmlNode> xnAttributeValList = new List<DOM.XmlNode>();
    for(Dom.XmlNode xn : xnAttributeList){
         //Check Name attibute matches the one you want
         if(xn.getAttribute('Name','') == 'http://schemas.microsoft.com/ws/2008/06/identity/claims/groups'){
            xnAttributeValList = xn.getChildElements();
            for(Dom.XmlNode xnv : xnAttributeValList){
                System.debug(xnv.getText());
            }
         }
    }

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .