Tuesday, April 8, 2008

Create XML in PHP

// here we are creating a xml document
$doc = new DomDocument('1.0');

//there can be only single root node in an xml, hence we are creating the same
$root = $doc->createElement('product');
$root = $doc->appendChild($root);

//we are setting the attribute of the root node
$root->setAttribute('xmlns:xsi','http://www.w3.org/2001/XMLSchema-instance');

//this is creating a node named as categoryHeirarchy within the root node
$ch = $doc->createElement('categoryHierarchy');
$ch = $root->appendChild($ch);

//this is creating a node named as categoryCode within the categoryHeirarchy node
$cat11 = $doc->createElement('categoryCode');
$cat11 = $ch->appendChild($cat11);
$cat11->setAttribute('code','1');
$cat11->setAttribute('name','division');

this xml will look like.....

<product
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<categoryHierarchy>
<categoryCode code="1" name="division">
</categoryCode>
</categoryHierarchy>
</product>





No comments:

 

blogger templates | Make Money Online