I am new here and to PHP, i am trying to write a xml file whitch had as source a XML file.
When i read the XML and use the echo command I can see al the record of the source XML.
but when i want to write it to a XML file i can only see one record.
I have read al lot of things already:
some thing i found was the use array’s, the use of xpath, or simpleXML.
For the code in echo part i use foreach is this possible with DOM elements?
The code is: `
$objDOM = new DOMDocument();
$dom1 = new DOMDocument();
$objDOM->preserveWhiteSpace = false;
$objDOM->load(“googlepoints.xml”); //make sure path is correct
$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"));
}
// save tree to file
$dom1->save(“order12.xml”);
//// save tree to string
//$dom1 = $dom1->save("order77.xml");
?>`
Out put of the XML file<?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>
Only there suppose the be 5 of this records.
Can sombody help me ?
The
$dom1variable needs to be set outside of the foreach loop.i.e.
It’s only one record because you’re overwriting the
$dom1each time where you have the comment// create doctype