最新消息:XAMPP默认安装之后是很不安全的,我们只需要点击左方菜单的 "安全"选项,按照向导操作即可完成安全设置。

PHP开发之生成XML的四种方式

XAMPP案例 中文小张 534浏览 0评论

使用纯粹的PHP代码生成字符串,并把这个字符串写入一个以XML为后缀的文件

function create_item($titleData, $titleSize, $contentData, $pubdateData)
{
$item = “<item>\n”;
$item .= “<title size=\”” . $titleSize . “\”>” . $titleData . “</title>\n”;
$item .= “<content>” . $contentData . “</content>\n”;
$item .= “<pubdate>” . $pubdateData . “</pubdate>\n”;
$item .= “</item>\n”;
return $item;
}

$dataArray = [
[
‘title’ => ‘title1’,
‘content’ => ‘content1’,
‘pubdate’ => ‘2009-10-11’
],
[
‘title’ => ‘title2’,
‘content’ => ‘content2’,
‘pubdate’ => ‘2009-11-11’
]
];
$titleSize = 1;
$xml = “<?xml version=\”1.0\” encoding=\”utf-8\”?>\n”;
$xml .= “<article>\n”;
foreach ($dataArray as $data) {
$xml .= create_item($data[‘title’], $titleSize, $data[‘content’], $data[‘pubdate’]);
}
$xml .= “</article>\n”;
echo $xml;

2.使用DomDocument生成XML文件,创建节点使用createElement方法,创建文本内容使用createTextNode方法,添加子节点使用appendChild方法,创建属性使用createAttribute方法

// $$ 可变变量,例:$one = ‘hello’; $hello = ‘world’; echo $one . ‘ ‘ . $$one; 结果为:hello world
function create_item($dom, $item, $data, $attribute)
{
if (is_array($data)) {
foreach ($data as $key => $val) {
// 创建元素
$$key = $dom->createElement($key);
$item->appendchild($$key);
// 创建元素值
$text = $dom->createTextNode($val);
$$key->appendchild($text);
if (isset($attribute[$key])) {
// 如果此字段存在相关属性需要设置
foreach ($attribute[$key] as $akey => $row) {
// 创建属性节点
$$akey = $dom->createAttribute($akey);
$$key->appendchild($$akey);
// 创建属性值节点
$aval = $dom->createTextNode($row);
$$akey->appendChild($aval);
}
}
}
}
}

$dataArray = [
[
‘title’ => ‘title1’,
‘content’ => ‘content1’,
‘pubdate’ => ‘2009-10-11’
],
[
‘title’ => ‘title2’,
‘content’ => ‘content2’,
‘pubdate’ => ‘2009-11-11’
]
];
// 属性数组
$attributeArray = [‘title’ => [‘size’ => 1]];
// 创建一个XML文档并设置XML版本和编码
$dom = new DomDocument(‘1.0’, ‘utf-8’);
// 创建根节点
$article = $dom->createElement(‘article’);
$dom->appendchild($article);
foreach ($dataArray as $data) {
$item = $dom->createElement(‘item’);
$article->appendchild($item);
create_item($dom, $item, $data, $attributeArray);
}
echo $dom->saveXML();

3.使用XMLWriter类创建XML文件,此方法在PHP 5.1.2后有效.另外,它可以输出多种编码的XML,但是输入只能是utf-8

$dataArray = [
[
‘title’ => ‘title1’,
‘content’ => ‘content1’,
‘pubdate’ => ‘2009-10-11’
],
[
‘title’ => ‘title2’,
‘content’ => ‘content2’,
‘pubdate’ => ‘2009-11-11’
]
];
// 属性数组
$attributeArray = [‘title’ => [‘size’ => 1]];
$xml = new XMLWriter();
$xml->openUri(“php://output”);
// 输出方式, 也可以设置为某个xml文件地址, 直接输出成文件
$xml->setIndentString(‘ ‘);
$xml->setIndent(true);
$xml->startDocument(‘1.0’, ‘utf-8’);
// 开始创建文件, 根结点
$xml->startElement(‘article’);
foreach ($dataArray as $data) {
$xml->startElement(‘item’);
if (is_array($data)) {
foreach ($data as $key => $row) {
$xml->startElement($key);
if (isset($attributeArray[$key]) && is_array($attributeArray[$key])) {
foreach ($attributeArray[$key] as $akey => $aval) {
// 设置属性值
$xml->writeAttribute($akey, $aval);
}
}

$xml->text($row);
$xml->endElement();
}
}
$xml->endElement();
}
$xml->endElement();
$xml->endDocument();
$xml->flush();

4.使用SimpleXML创建XML文档

$dataArray = [
[
‘title’ => ‘title1’,
‘content’ => ‘content1’,
‘pubdate’ => ‘2009-10-11’
],
[
‘title’ => ‘title2’,
‘content’ => ‘content2’,
‘pubdate’ => ‘2009-11-11’
]
];
// 属性数组
$attributeArray = [‘title’ => [‘size’ => 1]];

// 第一种
$string = <<<XML
<?xml version=”1.0″ encoding=”utf-8″?>
<article>
</article>
XML;
$xml = simplexml_load_string($string);

// 第二种
$xml = new SimpleXMLElement(‘<?xml version=”1.0″ encoding=”utf-8″?><article></article>’);
foreach ($dataArray as $data) {
$item = $xml->addChild(‘item’);
if (is_array($data)) {
foreach ($data as $key => $row) {
$node = $item->addChild($key, $row);
if (isset($attributeArray[$key]) && is_array($attributeArray[$key])) {
foreach ($attributeArray[$key] as $akey => $aval) {
// 设置属性值
$node->addAttribute($akey, $aval);
}
}
}
}
}
echo $xml->asXML();

转载请注明:XAMPP中文组官网 » PHP开发之生成XML的四种方式

您必须 登录 才能发表评论!