Showing posts with label code. Show all posts
Showing posts with label code. Show all posts

Monday, July 20, 2009

ColdFusion Program to Read XML File

Markup encodes and transfers metadata about information such as its structure and format. XML is a markup language, derived from the much earlier SGML, which uses strings of short words to surround the data it is describing. These strings of short words are known as tags. An example would be <name>George</name> <phone>555-1212</phone>.

With XML, a structural model of the data in a file can be encoded along with the data. The structure can be as simple as name and phone or much more complex, with fields like name and phone embedded in other fields like employee. A style sheet, an XSL, can be used to transform the tagged data in an XML file into a Web page. Likewise, a schema file, an XSD, can communicate database information in database operations.

ColdFusion provides a subset of functions that enable a programmer to operate on XML files. A typical operation would be to read an XML file, work on it and write it, perhaps to a database. A datatype in ColdFusion has been created for XML and is known as the XML document object. By doing this, Adobe extends the reach of its already existing ColdFusion structure functions to encompass XML data as well.

Structures consist of objects, properties, and objects embedded in other objects. Name and phone embedded in employee is an example. The dot operator is used to delimit what object in a structure you want to access. The general syntax is <object>.<object>, <object>.<property>. These can be combined to get for instance <object>.<child object>.<property>, so to access my phone number, we would use the dot operator: <employee>.<phone>

The following ColdFusion code uses some of the XML functions to read an XML file. It starts by validating the XML data file to ensure that it is consistent with its schema.

<cfset myResults= XMLValidate ("wits.xml", "wits.xsd")>
<cfoutput> Is Valid? #myResults.status#!
</cfoutput>


Next, a file handle is obtained.

<cffile variable="gmrXML" file=" “wits.xml" action="read">

Then we get a ColdFusion XML Document Object datatype using our file handle and the xmlParse() function:

<cfset myxml=" xmlParse(gmrXML)">

Now we are ready to loop through the data, and display different field values. This test file consists of a series of incidents embedded in an incident list. In addition to its own properties, an incident will have an embedded list, or object, of one or more incident types. In addition to this, it will have, an embedded facilities list, of zero or more facilities involved in the incident. These embedded lists correspond to embedded objects or to child tables in a database.


<cfoutput>
<cfloop index="i" from="1" to="#arrayLen(myXML.incidentList.Incident)#">
Incident<br>
#myXml.incidentlist.incident[i].ICN#<br>
#myXml.incidentlist.incident[i].Subject#<br>
#myXml.incidentlist.incident[i].Summary#<br>
#myXml.incidentlist.incident[i].IncidentDate#<br>
Event Type<br>
<!---Here is an inner loop to get all Event Types for the Incident--->
<cfloop index="j" from="1" to=
"#arrayLen(myXML.incidentList.Incident[i]. EventTypeList.EventType)#">
#myXml.incidentlist.incident[i].EventTypeList.EventType[j]#<br>
</cfloop>
<br>Facilities<br>
<cfif StructKeyExists(myXml.incidentlist.incident[i].FacilityList,"Facility")>
<!---Here is an inner loop to get all Facilities for the Incident--->
<cfloop index="j" from="1" to
"#arrayLen(myXML.incidentList.Incident[i]. FacilityList.Facility)#">
#myXml.incidentlist.incident[i].FacilityList.Facility[j].FacilityType#<br>
#myXml.incidentlist.incident[i].FacilityList.Facility[j].Indicator#<br>
</cfloop>
<cfelse>
No facility for this one
</cfif>
</cfloop>
</cfoutput>



The ColdFusion arraylen is used in the “To” parameter of our loop to return the number of Incidents in this incidentlist. There is one incidentlist per file. Our dot operator starts with the file handle, then refers to the incidentlist we know to be in file (because we validated), and the particular incident is referenced with our index variable [i]:

myXml.incidentlist.incident[i].ICN

The incident number property, ICN, is accessed from the record just read from the file. For EventType, it is very similar. Since we know we will have at least one Event Type in every incident (one or more), the following code is sufficient:

myXml.incidentlist.incident[i].EventTypeList.EventType[j]

On the other hand, we may not have a facility (zero or more) and will get a null pointer exception if we try to dereference a facility in an empty facilitylist. We need to use the ColdFusion StructKeyExists function to test if a facility object is embedded in the facilitylist for the current record.

<cfif StructKeyExists(myXml.incidentlist.incident[i].FacilityList,"Facility")>

If so, then we loop through the facilities involved in the incident, else we indicate no facility.

Friday, June 20, 2008

Fractal Art in Mobile Marketing

Fractals provide useful video art for mobile marketing. Large and amorphous patterns crystallize into smaller, distinct shapes. Or the logistic equation can be graphed in reverse from chaos to tipping point to order. The advantage of these as dynamic logos is that a small code module can be downloaded to create them rather than the download of a large video. The downside would be security concerns.

An example is the Focusing Triangles Fractal with code included in this post.




'******************************************************************************************
' This program creates a diffuse pattern and focuses it to a crisp, smaller triangle.
' Author: George Ray
'******************************************************************************************
Option Explicit
Dim iGMR, RandNum As Integer
Dim gmrDenom As Double
Dim gmrAx, gmrAy, gmrBx, gmrBy, gmrCx, gmrCy, gmrPx, gmrPy As Double
Sub Main()
gmrDisplay.BackColor = QBColor(0)
gmrDisplay.Show
Call gmrLogo
End Sub

Sub gmrLogo()
gmrAx = 5000 / 4
gmrAy = 3000 / 4
gmrBx = 4000 / 4
gmrBy = 5000 / 4
gmrCx = 6000 / 4
gmrCy = 5000 / 4
gmrPx = 5000 / 4
gmrPy = 4000 / 4

gmrDisplay.PSet (gmrAx, gmrAy), RGB(255, 0, 0)
gmrDisplay.PSet (gmrBx, gmrBy), RGB(255, 0, 0)
gmrDisplay.PSet (gmrCx, gmrCy), RGB(255, 0, 0)

gmrDenom = 1.5
Randomize
For gmrDenom = 1.2 To 2.2 Step 0.4
For iGMR = 1 To 10000
RandNum = Int(3 * Rnd + 1)
If RandNum = 1 Then
gmrPx = (gmrAx + gmrPx) / gmrDenom
gmrPy = (gmrAy + gmrPy) / gmrDenom
gmrDisplay.PSet (gmrPx, gmrPy), RGB(255, 0, 0)
ElseIf RandNum = 2 Then
gmrPx = (gmrBx + gmrPx) / gmrDenom
gmrPy = (gmrBy + gmrPy) / gmrDenom
gmrDisplay.PSet (gmrPx, gmrPy), RGB(0, 255, 0)
ElseIf RandNum = 3 Then
gmrPx = (gmrCx + gmrPx) / gmrDenom
gmrPy = (gmrCy + gmrPy) / gmrDenom
gmrDisplay.PSet (gmrPx, gmrPy), RGB(0, 0, 255)
End If
Next iGMR
Next gmrDenom
End Sub

If you have difficulty with the code, please post a comment and I will try to help.

The Backend Mechanics of SMS Mobile Marketing

The Short Message Service (SMS) is a network protocol for sending short, usually text messages to wireless devices . Sarah Perez (see Perez on Mobile Mktg),says that SMS is a growing trend in m-commerce. SMS has many features that support m-commerce, which include (see Adobe on SMS)

1. Built-in Authentication
2. Secure Communications
3. Interactive Communications
4. Mobile devices already include the client

Cell phone carriers, such as Sprint or Nextel, provide SMS gateways for the transfer of text messages from computer to mobile phone. These gateways are the foundation of the mechanics for an organization, be it an aggregator mobile marketing service or a commercial business entity, to implement a mobile marketing campaign. As mBlox notes (2006, p 6, see mBlox on SMS), there are over “a dozen US-based carriers to serve all their customers. As such, SMS connectivity provided by any single carrier would be incomplete.”

The solution is to determine the carriers that service the customer publics targeted by the campaign. Their SMS gateways are published and provide the hook for a database driven campaign to reach the customer. For example, Wikipedia has a list of mobile phone SMS gateways by carrier (see SMS Gateways in Wikipedia).

Duncan (2005, p 392), says an important aspect of mobile marketing is that “messages can be targeted not only by individual cellular phone number but also by time and location of targeted customers.” Having a relevant customer database is critical, so that demographic and psychographic characteristics can be used to customize message content and to insure that only customers interested in the message are included in the distribution.

An example data retrieval from a customer database using the ColdFusion language common for Web projects is:

(cfquery name="gmrMobile" datasource="heat")
SELECT
LastName, FirstName, CarrierGateway, Phone, Demographic1, DemographicN, Psychographic1, PsychographicN

FROM dbo.Customer

WHERE
dbo.Customer.SelectCharacterisitic = #CampaignCharacterisitic# and dbo.CustomerOptIn = ‘YES’
(/cfquery)

To send the message from our computer based application to the customer cell phone, we need to combine the cell phone number and carrier to form the correct gateway address. The process using our results above is:

(cfset gmrgateway=" #gmrMobile.Phone#" + “@” + #gmrMobile.CarrierGateway#)

For example,

Assuming my phone number is 800-555-1212 and my carrier is Sprint, the Sprint SMS gateway is messaging.sprintpcs.com according to Wikipedia. So my email from the computer would be sent to the gateway

8005551212@messaging.sprintpcs.com

which in turn forwards it to the SMS client on my cell phone.

A function would be called for each customer record returned from our database call in a loop. The function call looks like:

(cfinvoke component="Your.Component.Name" returnvariable="SendMailSimpleRet" method="SendMailSimple")
(cfinvokeargument name="strTo" value=" #gmrGateway# ")
(cfinvokeargument name="strFrom" value="Your_Name@Your.Company ")
(cfinvokeargument name="strSubject" value="Your Mobile Topic")
(cfinvokeargument name="strBody" value="Your Mobile Message")
(/cfinvoke)

The “your mobile message” in red would be formulated based on the customer demo and psychographics.

The code for the SendMailSimple function above that sends the mail to the SMS gateway is
(cffunction name="SendMailSimple" hint="Send Mail Simple" displayname="SendMailSimple" returntype="string" output="true" access="remote")
(cfargument name="strTo" default="" required="true")
(cfargument name="strFrom" default="" required="true")
(cfargument name="strSubject" default="" required="true")
(cfargument name="strBody" default="" required="true")

(cfmail from="#ARGUMENTS.strFrom#" to="#ARGUMENTS.strTo#" subject="#ARGUMENTS.strSubject#")
#ARGUMENTS.strBody#
(/cfmail)

(cfreturn)
(/cffunction)

References
Duncan, Tom (2005). Principles of Advertising and IMC. New York: McGraw-Hill Irwin.

Sunday, June 1, 2008

SMS Sender

The SMS Sender is javascript that formats an e-mail from your computer for delivery to your phone company's sms-email gateway. That gateway then forwards the email as a text message to your cell phone. On your cell phone, if you reply to that text message, it goes back to the gateway and is converted to an e-mail and sent to your email service.

It shows the mechanics of how a computer can send and receive messages from a cell phone and this is the essence of how sms marketing is done, except this is using your email client for demo purposes.

The javascript code is:

(SCRIPT LANGUAGE="JavaScript")(!--
function getLink(text) {
for (var i=0; i(document.links.length; i++)
if (document.links[i].href == text) return i;
return null;
}

function gmrAddr() {
var output = 'mailto:';
for (var i=0; i(document.smsGateways.selectName.length; i++) {
if (document.smsGateways.selectName[i].selected) {
if (output == 'mailto:')
output += '?to=' + document.smsGateways.selectName[i].value;
else
output += '&to=' + document.smsGateways.selectName[i].value;
}
}
document.links[mailtoLink1].href = output;
}
//--)(/SCRIPT)

(FORM NAME="smsGateways")
(select name="selectName")
(option value="@messaging.sprintpcs.com")Sprint Mobile(/option)
(option value="@vmobl.com")Virgin Mobile(/option)
(option value="@vtext.com")Verizon Mobile(/option)
(option value="@teleflip.com")Teleflip(/option)(/select)
(/FORM)

(A HREF="mailto:gmr" onClick="gmrAddr()")Send Mail(/A)

(SCRIPT LANGUAGE="JavaScript")(!--
var gmrMail= getLink('mailto:gmr');
//--)(/SCRIPT)