PHP code example of jan-drda / pure-php-xml-writer

1. Go to this page and download the library: Download jan-drda/pure-php-xml-writer library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

jan-drda / pure-php-xml-writer example snippets


/**
 * Composer autoload (only if you do not use it anywhere else)
 *
 * It is needed for namespace mapping
 */
iter('feed.xml');

/**
 * Open root element "items" (true = expecting children elements)
 */
$xmlWriter->openXMLElement('products', true);

/**
 * Save simple product
 */
$xmlWriter->openXMLElement('product', true); // Open the parent element
$xmlWriter->saveElementWithValue('name', 'Breakfast white mug'); // Name
$xmlWriter->saveElementWithValue('description', 'Nice white mug used for breakfast'); // Description
$xmlWriter->saveElementWithValue('price', 5.00, 2); // Price with 2 decimals
$xmlWriter->saveElementWithValue('category', 'Mugs|Breakfast'); // Category
$xmlWriter->saveElementWithValue('quantity', 20); // Quantity available
$xmlWriter->closeXMLElement('product'); // Close the parent element

/**
 * /Save simple product
 */

/**
 * Save variable product where variants have individual prices
 */
$xmlWriter->openXMLElement('product', true); // Open the parent element
$xmlWriter->saveElementWithValue('name', 'Puma T-shirt'); // Name
$xmlWriter->saveElementWithValue('description', 'Puma t-shirt with some sizes'); // Description
$xmlWriter->saveElementWithValue('price', 10.00, 2); // Price with 2 decimals
$xmlWriter->saveElementWithValue('category', 'T-shirts|Nike'); // Category
$xmlWriter->saveElementWithValue('quantity', 10); // Quantity available
$xmlWriter->openXMLElement('sizes', true); // Open the parent element for sizes

// Small size
$xmlWriter->openXMLElement('size', true); // Open the parent element for size
$xmlWriter->saveElementWithValue('size_name', 'S'); // Size name
$xmlWriter->saveElementWithValue('size_price', 10.00); // Size price with 2 decimals
$xmlWriter->closeXMLElement('size', true); // Open the parent element for size

// Medium size
$xmlWriter->openXMLElement('size', true); // Open the parent element for size
$xmlWriter->saveElementWithValue('size_name', 'M'); // Size name
$xmlWriter->saveElementWithValue('size_price', 11.00); // Size price with 2 decimals
$xmlWriter->closeXMLElement('size', true); // Open the parent element for size

// Large size
$xmlWriter->openXMLElement('size', true); // Open the parent element for size
$xmlWriter->saveElementWithValue('size_name', 'L'); // Size name
$xmlWriter->saveElementWithValue('size_price', 13.00); // Size price with 2 decimals
$xmlWriter->closeXMLElement('size', true); // Open the parent element for size
$xmlWriter->closeXMLElement('sizes', true); // Close the parent element for sizes
$xmlWriter->closeXMLElement('product'); // Close the parent element

/**
 * /Save variable product
 */

/**
 * Close root element "items"
 */
$xmlWriter->closeXMLElement('products');