工作中要用到XML,生成源码:
$dom = new DOMDocument(); if(!$dom) { printf("构造XML失败!\n"); return -3999; }
if(FALSE == ($dom->load($filename))) { add_root(); if(FALSE == ($dom->load($filename))) { return -3999; } }
//引用root根节点 $f_node = $dom->getElementsByTagName($parent_node)->item(0); if(!$f_node) { $root = $dom->getElementsByTagName('root')->item(0); //创建父节点 $f_node = $dom->createElement($parent_node); $f_node = $root->appendChild($f_node); } //创建节点 $item_node = $dom->createElement($node); $item_node = $f_node->appendChild($item_node); $item_value = $dom->createTextNode($value); $item_node->appendChild($item_value); $dom->save($filename);
|
生成结果:
<?xml version="1.0" encoding="utf-8"?> <root><node1>a1</node1><node2>a2</node2><node3>a3</node3></root>
|
,不是我想要的,改改,仅仅在load之前添加了,两行代码:
$dom = new DOMDocument(); if(!$dom) { printf("构造XML失败!\n"); return -3999; } $dom->preserveWhiteSpace = false; //就是这两行起作用啦,哈哈 $dom->formatOutput = true;//就是这两行起作用啦,哈哈 if(FALSE == ($dom->load($filename))) { add_root(); if(FALSE == ($dom->load($filename))) { return -3999; } }
|
呦呵,看结果:
<?xml version="1.0" encoding="utf-8"?> <root> <node1>a1</node1> <node2>a2</node2> <node3>a3</node3> </root>
|
详情请看:
阅读(2217) | 评论(0) | 转发(0) |