Skip to content

Consuming the data produced by XMLNuke

Joao Gilberto Magalhães edited this page Apr 17, 2014 · 4 revisions

XMLNuke by default will try transform the XML produced in HTML by using the XSL. But you can get the raw data in XML or JSON by adding in the URL the parameter raw followed by xml or json.

The example bellow will try transform using XSLT a module called CountryList

http://my.server/xmlnuke.php?module=sample.countrylist

The example bellow will get the CountryList result in a JSON

http://my.server/xmlnuke.php?module=sample.countrylist&raw=json

or XML:

http://my.server/xmlnuke.php?module=sample.countrylist&raw=xml

Creating the Classes

To ilustrate this I will create a XMLNuke module that list some countries or the cities of the selected country:

<?php
class CountryList 
{
	protected $_countries = array();
	public function getCountries()
	{
		return $this->_countries;
	}
     
	public function addCountry($object)
	{
		$this->_countries[] = $object;
	}
}

class Country
{
	public $ID;
        public $Name;
	public function __construct($id, $name)
	{
		$this->ID = $id;
		$this->Name = $name;
	}
}

class CityList 
{
	protected $_cities = array();
	public function getCities()
	{
		return $this->_cities;
	}
     
	public function addCity($object)
	{
		$this->_cities[] = $object;
	}
}

class City
{
	public $ID;
        public $Name;
	public function __construct($id, $name)
	{
		$this->ID = $id;
		$this->Name = $name;
	}
}


class ListCountries extends BaseModule
{
	/**
	*@desc Default constructor
	*/
	public function __construct()
	{}

	public function CreatePage() 
	{
		$this->defaultXmlnukeDocument = new XmlnukeDocument('', '');

                // Get the ID 
		$id = $this->_context->Value('id');


                // If the parameter ID was not passed get the Country List
		if ($id == '')
		{
			$country = new CountryList();
			$country->addCountry(new Country(1, 'Brazil'));
			$country->addCountry(new Country(2, 'United States'));
			$country->addCountry(new Country(3, 'Canada'));
			$this->defaultXmlnukeDocument->addXmlnukeObject($country); // <---
		}
		else
		{
			$city = new CityList();
			switch ($id)
			{
				case 1:
					$city->addCity(new City(2, 'Rio de Janeiro'));
					$city->addCity(new City(3, 'Sao Paulo'));
					break;
				case 2:
					$city->addCity(new City(5, 'Los Angeles'));
					$city->addCity(new City(6, 'Miami'));
					break;
				case 3:
					$city->addCity(new City(8, 'Vancouver'));
					$city->addCity(new City(9, 'Toronto'));
					break;
			}
			$this->defaultXmlnukeDocument->addXmlnukeObject($city); // <---
		}

		return $this->defaultXmlnukeDocument;
	}
	
}
?>

This code contains the classes and collections for create and group the Country and List. The module will check if the ID parameter was passed or not and create the proper class for Country or City. This module does not requires anything else special and the content will be the content classes.

So, calling:

http://my.server/xmlnuke.php?module=sample.ListCountries&raw=json&xpath=//Country

I will get:

{
   "Country": [
      {
         "ID": "1",
         "Name": "Brazil"
      },
      {
         "ID": "2",
         "Name": "United States"
      },
      {
         "ID": "3",
         "Name": "Canada"
      }
   ]
}

And

http://my.server/xmlnuke.php?module=sample.ListCountries&raw=json&xpath=//City&id=1

I will get

{
   "City": [
      {
         "ID": "2",
         "Name": "Rio de Janeiro"
      },
      {
         "ID": "3",
         "Name": "Sao Paulo"
      }
   ]
}

Consuming the Data

In this example will consume the data in a JSON request. To do this we have a select tag with the countries get from the XMLNuke module and on change the item get the associated city using JSON too.

The HTML code will be:

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.10.2/jquery.min.js"></script>

<select id="country" name="id"></select>

<span id='cities'></span>

and the JavaScript

<script type='text/javascript'>
    $(function(){
    $.getJSON("http://my.server/xmlnuke.php", {module: 'sample.ListCountries', raw: "json", xpath: '//Country'}, function(data){
	   var $country = $("#country");
           $country.empty();
           $country.append($('<option></option>').attr("value", "").text('-- Select One --'));
           $.each(data.Country, function () {
               $country.append($('<option></option>').attr("value", this.ID).text(this.Name));
           });
       });
    });


    $("#country").change(function(){
       $.getJSON("http://my.server/xmlnuke.php", {module: 'sample.ListCountries', raw: "json", xpath: '//City', id: $(this).val()}, function(data){
	  var $cities = $("#cities");
          $cities.empty();

	  if (data.City == undefined)
		return;

          var options = '';
          $.each(data.City, function () {
              options += '<option value="' + this.ID + '">' + this.Name + '</option>';
          });

          $("#cities").html('<select id="city">' + options + '</select>');

       });
    });
</script>
Clone this wiki locally