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!