Examples of Import/Export

The following code example iterates over the products that are currently configured in the site and create an XML document (object), so this data can be passed to another system.

This is the code that performs the iteration:

var it : SeekableIterator = ProductMgr.queryAllSiteProducts(); 
var rootXML : XML = new XML(<products></products>); 
var pXML : XML = new XML(<product></product>); 
while(it.hasNext()){ 
  var product : Product = it.next(); 
  pXML.brand = product.getBrand(); 
  pXML.name = product.getName(); 
  pXML.id = product.getID(); 
 rootXML.product+=pXML; 
} 

The process is as follows:

  1. Create the product iterator to capture all the product data needed for the XML document.
  2. Create the root XML element products (object).
  3. Create an XML element (product) to be added to the document. This XML object only needs to be created once.
  4. Iterate over the product object, creating and adding to the product element.
  5. Add the complete product element to the root element with the following line:
	rootXML.product+=pXML

This line adds a new product element to the root element after each iteration.

If you wanted to add another element to the XML, use this code:

var it : SeekableIterator = ProductMgr.queryAllSiteProducts(); 
var rootXML : XML = new XML(<products></products>); 
var pXML : XML = new XML(<product></product>); 
var paymentXML = new XML(<payment></payment>); 
while(it.hasNext()){ 
  var product : Product = it.next(); 
  pXML.brand = product.getBrand(); 
  pXML.name = product.getName(); 
  pXML.id = product.getID(); 
 paymentXML.allowed =product.custom.allowed; 
 paymentXML.bml = product.custom.bml 
 rootXML.product+=pXML; 
 rootXML.payment+= paymentXML; 
 } 

In the previous code, the payment object is considered a custom attribute that needs to be in its own XML element.

In addition to adding new elements, you can also outline what data elements are allowed in the document element, as shown in the following code example:

var it : SeekableIterator = ProductMgr.queryAllSiteProducts(); 
var rootXML : XML = new XML(<products></products>); 
var pXML = <product> 
          <brand></brand> 
          <name></name>     
          <id></id> 
          </product> 
var paymentXML = new XML(<payment></payment>); 
while(it.hasNext()){ 
  var product : Product = it.next(); 
  pXML.brand = product.getBrand(); 
  pXML.name = product.getName(); 
  pXML.id = product.getID(); 
  paymentXML.allowed =product.custom.allowed; 
  paymentXML.bml = product.custom.bml 
 rootXML.product+=pXML; 
 rootXML.payment+= paymentXML; 
} 

The following example code shows how to add a new namespace to the XML document.

var nsg : Namespace = new Namespace("g","http://base.google.com/ns/1.0"); 
var it : SeekableIterator = ProductMgr.queryAllSiteProducts(); 
var rootXML : XML = new XML(<products></products>); 
var pXML = <product> 
          <brand></brand> 
          <name></name>     
          <id></id> 
          </product> 
var paymentXML = new XML(<payment></payment>); 
pXML.addNamespace(nsg); 
while(it.hasNext()){ 
  var product : Product = it.next(); 
  pXML.nsg::brand = product.getBrand(); 
 pXML.nsg::name = product.getName(); 
 pXML.nsg::id = product.getID(); 
 paymentXML.allowed =product.custom.allowed; 
 paymentXML.bml = product.custom.bml 
 rootXML.product+=pXML; 
 rootXML.payment+= paymentXML; 
}