简体   繁体   中英

converting array into customized xml

So, I have following array from the query :

(findpercentage is the name of the cfquery I have used)

<cfset pieData = ArrayNew(1)>

    <cfset dataItem =[ 'Open', '#findpercentage.OPENS#' ]>
    <cfset ArrayAppend(pieData, dataItem)>  

    <cfset dataItem =[ 'Bounce', '#findpercentage.BOUNCE#' ]>
    <cfset ArrayAppend(pieData, dataItem)>  

    <cfset dataItem =[ 'Deferred', '#findpercentage.DEFERRED_EVENT#' ]>
    <cfset ArrayAppend(pieData, dataItem)>  

    <cfset dataItem =[ 'Dropped', '#findpercentage.DROPPED#' ]>
    <cfset ArrayAppend(pieData, dataItem)>  

    <cfset dataItem =[ 'Delivered', '#findpercentage.Delivered#' ]>
    <cfset ArrayAppend(pieData, dataItem)>

    <cfset dataItem =[ 'Processed', '#findpercentage.Processed#' ]>
    <cfset ArrayAppend(pieData, dataItem)>      

    <cfset dataItem =[ 'Spamreport', '#findpercentage.Spamreport#' ]>
    <cfset ArrayAppend(pieData, dataItem)>  

The array looks like the following :

The array size can keep on growing or shrinking (For example, there can be 2 arrays sometime and 20 sometime).

Is there a way, from the above array, I can generate an XML like the following:

<?xml version="1.0" encoding="UTF-8"?>
<root>
    <item>
        <value>31.4164</value>
        <label>Open</label>
        <colour>FFFF10AA</colour>
    </item>
    <item>
        <value>3.2240</value>
        <label>Bounce</label>
        <colour>FFAA0AAA</colour>
    </item>
    <item>
        <value>0.2430</value>
        <label>Deferred</label>
        <colour>FF5505AA</colour>
    </item>
    <item>
        <value>1.2799</value>
        <label>Dropped</label>
        <colour>FF0000AA</colour>
    </item>
    <item>
        <value>31.5584</value>
        <label>Delivered</label>
        <colour>FF0000AA</colour>
    </item>
    <item>
        <value>32.2290</value>
        <label>Processed</label>
        <colour>FF0000AA</colour>
    </item>
    <item>
        <value>0.0217</value>
        <label>Spamreport</label>
        <colour>FF0000AA</colour>
    </item>
</root>

I will also need to figure out a way to generate unique color which I have hardcoded at this point of time in the color tag.

Reason I am converting it into XML is because the tool here understands only XML or JSON.

Please let me know.

Result of SerializeJSON on my cfquery with name findpercentage :

{
    "COLUMNS": [
        "TOTAL_EVENTS",
        "OPENS",
        "BOUNCE",
        "DEFERRED_EVENT",
        "DROPPED",
        "DELIVERED",
        "PROCESSED",
        "SPAMREPORT"
    ],
    "DATA": [
        [
            267526,
            31.4164,
            3.224,
            0.243,
            1.2799,
            31.5584,
            32.229,
            0.0217
        ]
    ]
}

I recently had to create an xml file from query results. Basically, I did it like this: First, create a variable with the query data in a more or less properly structured format.

<cfsavecontent variable="data">
<BurnRecords>
<cfoutput query="AllData">
more code
</cfoutput>
</BurnRecords>

Next, create another variable with proper xml format

<cfxml casesensitive="yes" variable="xmldata">
<cfoutput>#Replace(data, chr(10) & chr(13) & chr(10) & chr(13), chr(10), "all")#</cfoutput>
</cfxml>

Finally, write the xml data to a file.

<cffile action="write" nameconflict="overwrite" file="#FileName#" output="#xmldata#">

Edit begins here

In retrospect, I appear to have overengineered it. This would have probably sufficed.

<cfxml casesensitive="yes" variable="xmldata">
<BurnRecords>
<cfoutput query="AllData">
more code
</cfoutput>
</BurnRecords>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM