I know that we can get a string from XMLNode using a string and using String invoiceNumber = root.getChildElement('invoiceNumber', null).getText();
. But I need a decimal number from same XML. can anyone please let me know the code how to get that?
Thanks a Ton..
Ashok.
-
There's no direct method which can parse non string data from xml, as far as I can tell you. If you know the node type, you can always convert it to the required data type once you have parsed the contents. – Jayant Das Aug 27 '18 at 12:30
-
HI @Jayant I have a list/array of decimals in that XML. I can add them to a string[] using same above syntax but I am not getting an idea how to change that string[] list to decimal. if that is a single value, then I can use valueOf(String). Can you give me a clue. Thank you. – AshSFcloud Aug 28 '18 at 4:24
-
Can you provide the sample xml and how you are retrieving the value? – Jayant Das Aug 28 '18 at 5:08
If you have a look at the documentation on the various primitive types (i.e. Integer, Boolean, Decimal, etc...) you'll find that they have a valueOf()
method that takes a string.
The decimal class is no different. From the Decimal class documentation
valueOf(stringToDecimal)
Returns a Decimal that contains the value of the specified String. As in Java, the string is interpreted as representing a signed Decimal.
Signature
public static Decimal valueOf(String stringToDecimal)
Parameters
stringToDecimal Type: String
Return Value
Type: Decimal
Example
String temp = '12.4567'; Decimal myDecimal = Decimal.valueOf(temp);
-
I really appreciate your response. I am recovering from XML response body and also, the Decimal is under 4th childNode of the XML. I have almost 6 to 7 decimals of same variable and they varies. valueOf(String) can be useful but I can not use through 'child4.getElement(variable,null)' kind of coding. If there is a way, please let me know. Thank you. – AshSFcloud Aug 28 '18 at 4:20