d4ne d4ne - 2 months ago 9
PHP Question

Create a xml file using PHP

I'm trying to create a XML file using PHP which would contain the following format:

<outfits>
<outfit>
<head url="xxx" c="000" c2="000" z="1" />
</outfit>
</outfits>


But im confused about how i would add multiple values to a string line such as url, c, c2, z.

// Function to create the outfit xml file
function create_outfit_xml(){
$xml = new DomDocument('1.0', 'UTF-8');
$outfits = $xml->createElement('outfits');
$outfits = $xml->appenChild($outfits);
$outfit = $xml->createElement('outfit');
$outfit = $xml->appenChild($outfit);
}


How would i manage to create this?

Answer

Using the SimpleXMLElement Class would be much ideal in this case. Below is an example:

<?php

    function createOutfitXML(){
        $xml        = new SimpleXMLElement('<outfits></outfits>');
        $head       = $xml->addChild("head");
        $head->addAttribute("uri", "xxx");
        $head->addAttribute("c1", "000");
        $head->addAttribute("c2", "2");
        $head->addAttribute("z", "1");
        $outfit     = $xml->addChild("outfit", "jeans");
        $outfit->addAttribute("color", "red");
        $outfit->addAttribute("size", "32");
        $xml->asXML(__DIR__ . "/xml.xml");
    }
    createOutfitXML();

Now, imagine you have an array containing a list of Outfits that you want to convert to an XML Document using the Function above. Here's how that could work using the Function:

THE OUTFITS ARRAY:

<?php

    $outfitsArray   = [
        "jeans" => [
            [
                "size"  => '32',
                "color" => 'Dark Blue',
                "price" => 120.00,
                "brand" => 'Lee',
            ],
            [
                "size"  => '34',
                "color" => 'Black',
                "price" => 100.00,
                "brand" => 'Sean Jean',
            ],
            [
                "size"  => '30',
                "color" => 'White',
                "price" => 150.00,
                "brand" => 'Pepe',
            ],
        ],
        "shoes" => [
            [
                "size"  => '40',
                "color" => 'Black',
                "price" => 180.00,
                "brand" => 'Lee',
            ],
            [
                "size"  => '44',
                "color" => 'Brown',
                "price" => 200.00,
                "brand" => 'Sean Jean',
            ],
            [
                "size"  => '42',
                "color" => 'Dark Red',
                "price" => 240.00,
                "brand" => 'Versace',
            ],
        ]
    ];

THE ALGORITHM:

<?php
    function createOutfitXML($outfitsArray){
        $xml        = new SimpleXMLElement('<outfits></outfits>');
        $head       = $xml->addChild("head");
        $head->addAttribute("uri", "xxx");
        $head->addAttribute("c1", "000");
        $head->addAttribute("c2", "2");
        $head->addAttribute("z", "1");
        foreach($outfitsArray as $groupName=>$itemData){
            foreach($itemData as $k=>$v){
                $outfit     = $xml->addChild("outfit");
                $outfit->addAttribute("group", $groupName);
                foreach($v as $key=>$value){
                    $outfit->addAttribute($key, $value);
                }
            }
        }
        $xml->asXML(__DIR__ . "/xml.xml");
    }
    createOutfitXML($outfitsArray);

The code above would create an XML Document similar in content to this:

    <?xml version="1.0"?>
    <outfits>
        <head uri="xxx" c1="000" c2="2" z="1"/>
        <outfit group="jeans" size="32" color="Dark Blue" price="120" brand="Lee"/>
        <outfit group="jeans" size="34" color="Black" price="100" brand="Sean Jean"/>
        <outfit group="jeans" size="30" color="White" price="150" brand="Pepe"/>
        <outfit group="shoes" size="40" color="Black" price="180" brand="Lee"/>
        <outfit group="shoes" size="44" color="Brown" price="200" brand="Sean Jean"/>
        <outfit group="shoes" size="42" color="Dark Red" price="240" brand="Versace"/>
    </outfits>