CPAINT :: Cross-Platform Asynchronous INterface Toolkit

Please note: CPAINT nor this website is under active development.

Developer's Guide : Backend

Backend Guide: Pages

integrating CPAINT
implementation differences
generating XML
complex return-data
arbitrary character encodings

Returning More than One Value

That simple example was great, but most likely, you need to return more than one thing. For traditional application programming, you would normally have to return the results from the functions in an array. However, since we're sending this to the frontend, we can use nested XML tags.

Here's an example of a function that returns four different results, simulaneously:

PHP Example

  require_once("cpaint v2/");

  $cp = new cpaint():

  function multi_calc($num1, $num2) {
    global $cp;
    $add_result = $num1 + $num2;
    $subtr_result = $num1 - $num2;
    $multi_result = $num1 * $num2;
    $div_result = $num1 / $num2;

We could use the cpaint.set_attribute() method, like we did in the previous example, but that function sets the top-level XML tag, which wouldn't work in this situation. Instead, we're going to use the cpaint.add_node() method which will create our XML tree. In this case, we're going to call it four times, so each one of our results is in a new tag, under the top-level tag, which by default is named ajaxResponse. Then we'll call the cpaint.set_data() method for each of those newly created nodes and add the results to the XML tree.

PHP Example

  require_once("cpaint v2/");

  $cp = new cpaint():

  function multi_calc($num1, $num2) {
    global $cp;

    $add_result = $num1 + $num2;
    $subtr_result = $num1 - $num2;
    $multi_result = $num1 * $num2;
    $div_result = $num1 / $num2;

    $add_result_node =& $cp->add_node("add");
    $subtr_result_node =& $cp->add_node("subtr");
    $multi_result_node =& $cp->add_node("multi");
    $div_result_node =& $cp->add_node("div");



Assuming our input for the function was 2 and 4, the XML generated would look like:


Advanced XML

If you wanted to go one step further and add multiple levels to the XML tree and use attributes, see the API documentation for the functions available to you. 32,000 is the theoretical limit to the number of objects you can add to the tree. Just remember, each tag, even those with the same name, counts againsts this limit. Depending on the language you use, this limit may not apply to you.

Due to the frequency of requests regarding this issue a short and mainly uncommented example:
To product this XML structure as output...

<?xml version="1.0" encoding="UTF-8"?>
  <response id="valid">
    <offset id="X">10</offset>
    <offset id="Y">10</offset>
</colorPicker> need to connect the XML nodes in the CPAINT backend like this:

// change name of basenode

// create 'color' node
$color_node	=& $cp->add_node('color');

// create submodes that actually hold the color information(s)
$hex_node	=& $color_node->add_node('hex');
$red_node	=& $color_node->add_node('red');
$green_node	=& $color_node->add_node('green');
$blue_node	=& $color_node->add_node('blue');

// create a new result node containing success information and the initial coordinates
$response_node	=& $cp->add_node('response', 'valid');

// ...and its subnodes
$validity_node	=& $response_node->add_node('isValid');
$x_node		=& $response_node->add_node('offset', 'X');
$y_node		=& $response_node->add_node('offset', 'Y');

// now assign all values
$hex_node->set_data(sprintf('#%02x%02x%02x', $red, $green, $blue));

Note that the nested nodes from the XML are created by calling add_node() on the proper parent nodes. This method is not called on the CPAINT object $cp all the time!