迭代编写XML问题

时间:2009-08-11 19:38:13

标签: xml loops

我是新来的和PHP,我正在尝试编写一个xml文件,它作为XML文件的来源。

当我读取XML并使用echo命令时,我可以看到源XML的记录。

但是当我想将它写入XML文件时,我只能看到一条记录。

我已经阅读了很多东西: 我发现的一些东西是使用数组,使用xpath或simpleXML。 对于echo部分中的代码我使用foreach是否可以使用DOM元素?

代码是:`

$ objDOM = new DOMDocument();

$ dom1 =新DOMDocument();

$ objDOM-> preserveWhiteSpace = false;

$ objDOM->负载( “googlepoints.xml”); //确保路径正确

$ photo = $ objDOM-> getElementsByTagName(“photo”);

foreach($ photo as $ value){     $ album = $ value-> getElementsByTagName(“album”);     $ albu = $ album-> item(0) - > nodeValue;

$description = $value->getElementsByTagName("description");
$descriptio = $description->item(0)->nodeValue;

$title = $value->getElementsByTagName("title");
$titl = $title->item(0)->nodeValue;

$link = $value->getElementsByTagName("link");
$lin = $link->item(0)->nodeValue;

$guid = $value->getElementsByTagName("guid");
$gui = $guid->item(0)->nodeValue;

$gps = $value->getElementsByTagName("gps");
$gp = $gps->item(0)->nodeValue;

echo "$task :: $detail :: $albu :: $descriptio :: $titl :: $lin :: $gui :: $medi :: $gp  <br>";


// create doctype
$dom1 = new DOMDocument("1.0", 'utf-8');
$dom1->formatOutput = true;

    $collection = $dom1->appendchild($dom1->createElement("collection"));

        // create child element "photo"
        $photo = $collection->appendChild($dom1->createElement("photo"));


        // create child element "album"
        $album = $photo->appendChild($dom1->createElement("album"));
        $album->appendChild($dom1->createTextNode("$albu"));

        // create child element "description"
        $description = $photo->appendChild($dom1->createElement("description"));
        $description->appendChild($dom1->createTextNode("$descriptio"));

        // create child element "title"
        $title = $photo->appendChild($dom1->createElement("title"));
        $title->appendChild($dom1->createTextNode("$titl"));

        // create child element "link"
        $link = $photo->appendChild($dom1->createElement("link"));
        $link->appendChild($dom1->createTextNode("$lin"));

        // create text node
        $guid = $photo->appendChild($dom1->createElement("guid"));
        $guid->appendChild($dom1->createTextNode("$gu"));

        // create child element "gps"
        $gps = $photo->appendChild($dom1->createElement('gps'));
        //$gps->appendChild($dom1->createTextNode("$gp"));
        $gps->appendChild($dom1->createTextNode("$gp"));

}         //将树保存到文件         $ dom1-&GT;保存( “order12.xml”);

    //// save tree to string
    //$dom1 = $dom1->save("order77.xml");

&GT;`

输出XML文件<?xml version="1.0" encoding="utf-8"?> <collection> <photo> <album>Landschap</album> <description>Foto's genomen op een mooie koude winterdag.</description> <title>nil</title> <link>index.html</link> <guid></guid> <gps>22°39'5" N 5°40'54" E</gps> </photo> </collection> 只有那里有5个这样的记录。

sombody可以帮帮我吗?

1 个答案:

答案 0 :(得分:0)

$dom1变量需要在foreach循环之外设置。

$dom1 = new DOMDocument("1.0", 'utf-8');
$dom1->formatOutput = true;

foreach($photo as $value) {

     $dom1->appendChild(...);
     ...
}

$xml = $dom1->save();

这只是一条记录,因为每次有评论$dom1

时,您都会覆盖// create doctype