How to convert XML array text items into numbers in ColdFusion?

Viewed 112

I am having problems converting XML data into the appropriate data types using a function. I have read in an XML file using XmlParse(). Within that there is an array which I loop around. <Cfloop array=#i.Task# index="t"> My understanding is that the items in this array are XML text. I can display all the items with CFoutput no problem. One item in the array (BaseLineColor) is a colour. #t.BaseLineColor# However this colour value is a single decimal integer number of varying length. I have worked out the maths to convert this decimal number into R,G,B decimal values. All good so far. The problem is that if I try mathematical functions on BaseLineColor, then I get:

The value ?xml version="1.0" encoding="UTF-8"? BaseLineColor 255 /BaseLineColor cannot be converted to a number.

So OK I have tried a few methods to try and convert BaseLineColor to an integer but nothing works. Val() doesn't work. In fact I can't seem to convert it into any datatype. For example, here is me trying to make it a string - same error:

 <cfscript>
            Strbaselinecolor=toString(t.BaseLineColor);
            rdec=floor(Strbaselinecolor / 65536);
            gdec=floor((Srtbaselinecolor - rdec * 65536)/256);
            bdec=floor(Strbaselinecolor - rdec * 65536 - gdec * 256);
            writeOutput("#t.baselinecolor#: #Strbaselinecolor# red #rdec#, green #gdec#, blue #bdec#")
</cfscript>

What function should I be using? Am I supposed to be pre-processing the XML in some way before I can refer to some of these values as integers?

There are a lot of values in the XML data which are numbers (some integers and some floating point numbers) and so it is not just about these items that are colours but a more general problem with using any XML data that is not text. I have tried to find some reference material on this but have not found anything relevant so far. Yet I'm guessing this is a common issue when reading in XML files.

Thanks in advance for any help.

1 Answers

The error message is correct. The code is trying perform mathematical operations on something that is not a number, despite the fact that it may appear to be one in a browser... You're probably getting tripped up by how browsers handle tag based code like xml.

This code (incorrectly) shows the value of t.BaseLineColor as a simple number 255

<cfscript>
    t = xmlParse('<?xml version="1.0" encoding="UTF-8"?><BaseLineColor>255</BaseLineColor>');
    writeOutput(t.BaseLineColor);
</cfscript>

Runnable Example

However, using a browser's "Inspect Element" tool, reveals the value is actually an xml string. Since browsers treat anything enclosed in < and > as html tags, which aren't rendered, only the number 255 is visible on screen.

Inspect Element Result

writeDump() is more helpful here. It shows t.BaseLineColor as an xml node, and it's value is accessible through the xmlText attribute.

WriteDump of XML Node

That simple value can be used in mathematical operations.

<cfscript>
    t = xmlParse('<?xml version="1.0" encoding="UTF-8"?><BaseLineColor>255</BaseLineColor>');
    result = t.BaseLineColor.xmlText  / 65536 ;
    writeOutput( result );
</cfscript>

Runnable Example

Related