我正在尝试使用PHP创建一个包含以下格式的XML文件:
<outfits>
<outfit>
<head url="xxx" c="000" c2="000" z="1" />
</outfit>
</outfits>
但我很困惑如何将多个值添加到字符串行,如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);
}
我将如何创建这个?
答案 0 :(得分:0)
虽然使用DOM是&#34;正确的&#34;做到这一点的方式,DOM也是臀部的一个彻头彻尾的痛苦,并且是一种令人难以置信的生成XML的方式。如果你有简单的XML,而且你知道如何安全地生成它,那就把它当作一个字符串来对待你的生活更轻松:
$url = htmlspecialchars($url);
$c = htmlspecialchars($c);
$xml = <<<EOL
<outfits><outfit><head url="$url" c="$c" etc... /></outfit></outfits>
EOL;
答案 1 :(得分:0)
您可以创建属性。
$domAttribute = $xml->createAttribute('c');
$domAttribute->value = '200';
$domElement->appendChild($element);
答案 2 :(得分:0)
在这种情况下,使用SimpleXMLElement
类非常理想。以下是一个例子:
<?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();
现在,假设您有一个包含要使用上述函数转换为XML文档的Outfits列表的数组。以下是使用函数的方法:
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',
],
]
];
算法:
<?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);
上面的代码将创建一个类似于内容的XML文档:
<?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>