$name = $picture->appendChild($dom->createElement("name"));
$name->appendChild($dom->createNode("pic 1"));
接下来还要接着创建picture节点:
$picture = $pictures->appendChild($dom->createElement("picture"));
其实这些麻烦的事可以写个for循环来实现。
生成xml文件:
$dom->formatOutput = true;//设置格式化输出
$dom->save("erhsh.xml");//保存xml文件
三、读取xml文件。
1、还是定义一个DOM对象;$dom->new DomDocument();
2、加载xml文件:$dom->load("erhsh.xml");
3、按照节点的名字取得节点集合:$dom->getElementByTagName("pictures");
这种方法有点麻烦,参考文件:
//www.jb51.net/article/25853.htm
不过有一种我喜欢的方法:simplexml_load_file("erhsh.xml");
此方法可以把xml文件的内容转换成对象的形式,使用"->"结和"[]"很容易去的xml的内容;
但是在开发中还是遇到了一点问题:
当执行:print_r($xml->pictures);时输出的是一个 SimpleXMLElement 对象,([picture] => array([0]=>array(...)[1]=>array(...)));
再执行:print_r($xml->pictures->picture);输出的是n个分开的对象。
执行:print_r($xml->pictures->picture[0]->id);输出的还是一个对象。这就很不理解,应该是一个字符串。 最后网上说是“迭代对象”,
应该使用echo输出,print_r(), var_dump()输出不准确。参考地址://www.jb51.net/article/25852.htm
当然也可以修改xml的值通过这个方法。
写的很烂,仅供本人备忘。







