php生成XML文件步骤

步骤

1、实例化 DOMDocument类

2、创在节点

3、节点套节点

4、追加属性内容等

这是方法里面的全部内容:

        $doc = new \DOMDocument('1.0','utf-8');//引入类并且规定版本编码
        $grandFather = $doc->createElement("Grandfather");//创建节点
        $grandFather->setAttribute("ID","1");//给Grandfather增加ID属性
        $grandFather->setAttribute("name","王海洋");//给Grandfather增加NAME属性
        $father = $doc->createElement("Father");//创建节点
        $father->setAttribute("son","王海");//给Father增加SON属性
        $grandFather->appendChild($father);//讲Father放到Grandfather下
        $content = $doc -> createTextNode('我是测试的');//设置标签内容
        $father  -> appendChild($content);//将标签内容赋给标签
        $doc->appendChild($grandFather);//创建顶级节点
        $doc->save("C:/Users/k/Desktop/代码测试区/生成的XML文件/test.xml");//保存代码

浏览器打开文件:

猜你喜欢

转载自blog.csdn.net/wqzbxh/article/details/83304577