Tuesday 25 March 2014

PHP XML Creation

Create one .xml named as ofi.xml

<Productxxxxxxxxxxx>
    <ID></ID>
    <orderExternalReferenceID/>
    <submissionDate></submissionDate>
    <shopperPassword/>   
    <shopperInfo>
       
    </shopperInfo>
    <orderExtendedAttributes>
       
    </orderExtendedAttributes>
    <lineItemLevelRequest>
   
    </lineItemLevelRequest>
   
</Productxxxxxxxxxxx>


Then create php file abc.php

<?php

error_reporting(E_ALL);
$user_key_array = array('userID' => 'sdfsdfsdfs', 'externalReferenceID' => 'JD(234234', 'companyID' => 'TESTID',
    'loginID' => 'RSUSER', 'siteID' => 'rs.com');

$item_Value = array('item' => array('name' => '', 'value' => '', 'datatype' => ''));
$order_item_values = array('name' => '', 'value' => '', 'datatype' => '');

$shippingAddress = array('addressID' => '111', 'city' => '111', 'countryA2' => '111', 'country' => 'US', 'countryName' => 'United States', 'line1' => '1', 'line2' => 2, 'line3' => 3,
    'locationCode' => '454', 'name1' => '', 'name2' => '', 'phoneNumber' => '', 'postalCode' => '', 'state' => '', 'email' => '', 'faxPhone' => '',
    'companyName' => '', 'phoneNumber2' => '', 'countyName' => '', 'extendedAttributes' => $item_Value);

$billingAddress = array('addressID' => '111', 'city' => '111', 'countryA2' => '111', 'country' => 'US', 'countryName' => 'United States', 'line1' => '1', 'line2' => 2, 'line3' => 3,
    'locationCode' => '454', 'name1' => '', 'name2' => 'sdfsdf', 'phoneNumber' => 'sdfsdf', 'postalCode' => 'sdfsdf', 'state' => '', 'email' => '', 'faxPhone' => 'sdfasdfas',
    'companyName' => '', 'phoneNumber2' => 'sdfsdf', 'countyName' => 'sdfsdf', 'extendedAttributes' => $item_Value);


$productKey = array('productID' => 2222, 'externalReferenceID' => 32324234, 'companyID' => 'RER343', 'locale' => 'en');

$productInfo = array('productDataID' => 23423, 'mfrPartNumber' => '', 'shipperPartNumber' => 'sdfsd',
    'sku' => 23423, 'name' => '', 'platform' => '', 'year' => '', 'seats' => '', 'companyID' => '', 'exportCountry' => '', 'manufactureCountry' => '',
    'harmonizeCode' => '', 'eccn' => '', 'licenseException' => '', 'ccats' => '', 'extendedAttributes' => $item_Value);

$common_lineItem = array('currencyCode' => '','amount' => '');

$lineItemLevelPricing = array('unitPrice' => $common_lineItem, 'listPrice' => $common_lineItem, 'distributorPrice' => $common_lineItem, 'pricePerQty' => $common_lineItem, 'tax' => $common_lineItem, 'productTax' => $common_lineItem, 'shippingTax' => $common_lineItem,
    'shipping' => $common_lineItem, 'handling' => $common_lineItem, 'incentive' => $common_lineItem, 'reqLevelIncentivePerQuantity' => $common_lineItem, 'lineItemLevelIncentivePerQuantity' => $common_lineItem,
    'taxableFees' => $common_lineItem, 'taxOnTaxableFees' => $common_lineItem, 'nonTaxableFees' => $common_lineItem, 'recurringFee' => $common_lineItem, 'shippingBeforeDiscount' => $common_lineItem);

$lineItem = array('lineItemID' => '', 'lineItemExternalReferenceID' => '',
    'quantity' => '', 'preOrder' => '', 'preOrderReleaseDate' => '', 'RSITInfoArray' => '',
    'replacementInfo' => '', 'productKey' => $productKey, 'productInfo' => $productInfo, 'lineItemLevelPricing' => $lineItemLevelPricing,
    'lineItemExtendedAttributes' => $item_Value);


$master_array = array(
    array('shopperInfo' => array(
            array('userKey' => $user_key_array, 'firstName' => 'John', 'lastName' => 'Wiliam',
                'email' => 'john@rs.com', 'locale' => 'en', 'homePhone' => '343-34343-34343',
                'faxPhone' => '343-34343-34343', 'shippingAddress' => $shippingAddress,
                'billingAddress' => $billingAddress)
        )
    ),
    array('orderExtendedAttributes' => array(
            array('item' => $order_item_values)
        )
    ),
    array('lineItemLevelRequest' => array(
            array('lineItemID' => 'dfsdf', 'lineItemExternalReferenceID' => '', 'quantity' => '', 'preOrder' => '', 'preOrderReleaseDate' => '', 'RSITInfoArray' => '',
                'replacementInfo' => '', 'productKey' => $productKey, 'productInfo' => $productInfo, 'lineItemLevelPricing' => $lineItemLevelPricing,
                'lineItemExtendedAttributes' => $item_Value)
        )
    )
);

$tmmFile = 'ofi.xml';
$_xmlDoc = new DOMDocument('1.0', 'UTF-8');
$_xmlDoc->load($tmmFile);

function createCustomXml($xmlDoc, $master_array) {
     foreach ($master_array as $value_array) {
          foreach ($value_array as $key => $values) {
               $parent = $xmlDoc;
               $parentElement = $parent->getElementsByTagName($key)->item(0);
                   if(!is_array($values)) {
         $parentElement->nodeValue = $values;
          } else {
         foreach ($values as $value) {
          foreach ($value as $key => $value1) {
           recursiveElementCreation($key, $value1, $xmlDoc, $parentElement);
          }
         }
        }
          }
     }
}

function recursiveElementCreation($key, $value, $xmlDoc, $parentElement) {
     if (!is_array($value)) {
          $parentElement->appendChild($xmlDoc->createElement($key, $value));
     } else {
          $parentForChild = $parentElement->appendChild($xmlDoc->createElement($key));
          foreach ($value as $key1 => $value1) {
               recursiveElementCreation($key1, $value1, $xmlDoc, $parentForChild);
          }
     }
}

createCustomXml($_xmlDoc, $master_array);
$_xmlDoc->formatOutput = true;

print_r($_xmlDoc->saveXML());
exit;