function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
DDayDDay 

Return type of XMLDom's getElementsByTagName('xxx') ?

Hello,

 

I'm using the XMLDom class to parse some XML. I'm trying to set a variable to a list of tag names that I can loop through later. What is the correct return type I should set 'names' in the following code to?

 

 

names = responseXML.getElementsByTagName('names');

 

I've tried setting it to List<String> but I get the following error:

 

  Error: Compile Error: Illegal assignment from LIST:XMLDom.Element to LIST:String

 

Thanks,

Dan

Best Answer chosen by Admin (Salesforce Developers) 
JmBessonartJmBessonart

Hi, 

 List<XMLDOM.Element> is the correct declaration.

 

J.

All Answers

AishAish
getElementsByTagName has a return type of List<Element>. Which means your "names" variable in your code snippet will need to be of the aforementioned type. In your code any time you want to retrieve the string value from your element, you need to use the
 getValue() method on the specific Element object in the List you are creating.
Message Edited by Aish on 06-10-2009 02:52 PM
DDayDDay

Hi Aish,

 

Thanks for the response. I'm coming from a PHP background so having to declare return types is throwing me for a loop. If I change that line to:

 

 

List<Element> names = responseXML.getElementsByTagName('names'); or public List<Element> names = new List<Element>();

 

 I keep getting Compile Error: Invalid type: Element

 
The entire XMLDom class is in my apex classes section which has the Element class in it. This seems like such a basic thing but it's very frustrating.

 

Thanks,
Dan

 

JmBessonartJmBessonart

Hi, 

 List<XMLDOM.Element> is the correct declaration.

 

J.

This was selected as the best answer
DDayDDay

Thanks J, that did the trick!

 

Dan