Examples of Import/Export
We have provided examples of import and export code.
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 code 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:
- Create the product iterator that captures all the product data needed for the XML document.
- Create the root XML element products (object).
- Create an XML element (product) to be added to the document. This XML object only must be created once.
- Iterate over the product object, creating and adding to the product element.
- Add the complete product element to the root element with the following line:
rootXML.product+=pXML
This line adds a 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 must 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 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;
}