• Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
    Dedicated community for Japanese speakers
  • 한국 커뮤니티
    Dedicated community for Korean speakers
Exit
0

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

New Here ,
Jul 13, 2021 Jul 13, 2021

Copy link to clipboard

Copied

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.

Views

283

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 14, 2021 Jul 14, 2021

Copy link to clipboard

Copied

Dave, you'll really need to show us exactly what's in  t.BaseLineColor, as all here in the code you offer hinges on that.

 

And it's critical that we know EXACTLY what is in that, so please don't just cfoutput the var and share that. 🙂 If it had html or xml (perhaps unexpectedly), that would be ignored or processed by the browser--but could impact processing by those cfml functions. Instead, wrap the var in htmleditformat or htmlcodeformat when outputting it.

 

As for learning more about processing xml in cfml, there is indeed doc in that. But you'd be forgiven for not readily finding it.

 

First, it's not in the cfml reference, though. For some topics, that's like trying to learn to speak English from a dictionary. 🙂 This is such a topic which has many tags, functions, and special variable results that all interrelate and demand their own discussion as a group. And that's offered in the cf  developer's guide,  another cf doc--which would be over 3000 pages if printed.

 

As for finding its coverage of xml, I did a blog post on that that could help here:

 

https://www.carehart.org/blog/client/index.cfm/2011/5/10/where_to_learn_CFMLs_XML_capabilities

 

Yes, it's now 10 years old but the info is just as valuable now (in the old docs I point to. And I explain why it's hard to find in the new docs. )

 

Finally, for those really interested to learn about cf and xml processing, I link there to a still older resource--perhaps one of the best technical introductions to a topic I'd ever seen--introducing and showing how to make the most of cf and xml: a classic piece by Nate Weiss from now nearly 20 years ago.

 

Sadly, few people new to cf or new to CF and xml processing ever see these great docs and never realize the amazing power and ease of xml processing. (And some of the same concepts apply as well to json processing, making it all the more worth knowing, for a well-rounded cfml understanding.)

 

Let us know if that helps, and if perhaps you may even solve the problem on your own with my first tips above. 


/Charlie (troubleshooter, carehart.org)

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Jul 14, 2021 Jul 14, 2021

Copy link to clipboard

Copied

LATEST

From what I can see, there is nothing wrong with the numeric conversion. There is an error in the way in which you parse the XML. In particular, you've extracted a non-numeric string, and treated it as if it were numeric. 

 

Check by just outputting each of the variables, one by one. Without doing any math on them.

 

You will find the following: one of the variables you consider to be numeric is actually a string, such as '?xml version="1.0" encoding="UTF-8"? BaseLineColor 255 /BaseLineColor'. Hence the error message.

Votes

Translate

Translate

Report

Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Resources
Documentation