简体   繁体   中英

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?

While using DOM is the "proper" way of doing it, DOM is also a complete pain in the rump and is an incredibly bloated way to produce XML. If you have simple XML, and you're aware of how to produce it safely, just treat it as a string to make your life easier:

$url = htmlspecialchars($url);
$c = htmlspecialchars($c);

$xml = <<<EOL
<outfits><outfit><head url="$url" c="$c" etc... /></outfit></outfits>
EOL;

You can create an attribute.

$domAttribute = $xml->createAttribute('c');
$domAttribute->value = '200';
$domElement->appendChild($element);

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>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM