简体   繁体   English

使用PHP创建一个xml文件

[英]Create a xml file using PHP

I'm trying to create a XML file using PHP which would contain the following format: 我正在尝试使用PHP创建一个XML文件,其中包含以下格式:

<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. 但我很困惑如何将多个值添加到字符串行,如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. 虽然使用DOM是“正确”的方式,但DOM也是一个彻头彻尾的痛苦,并且是一种令人难以置信的生成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: 如果你有简单的XML,并且你知道如何安全地生成它,那么只需将其视为一个字符串,让你的生活更轻松:

$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. 在这种情况下,使用SimpleXMLElement类将非常理想。 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. 现在,假设您有一个包含要使用上述函数转换为XML文档的Outfits列表的数组。 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 Document:

    <?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>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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