- 使用纯粹的PHP代码生成字符串,并把这个字符串写入一个以XML为后缀的文件
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30function 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;
使用DomDocument生成XML文件,创建节点使用createElement方法,创建文本内容使用createTextNode方法,添加子节点使用appendChild方法,创建属性使用createAttribute方法
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51// $$ 可变变量,例:$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();使用XMLWriter类创建XML文件,此方法在PHP 5.1.2后有效.另外,它可以输出多种编码的XML,但是输入只能是utf-8
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43$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();使用SimpleXML创建XML文档
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40$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();