0

Im using CubeGallery3D, that stores with XML files.

The XML structure looks like this: http://pastebin.com/peMxcmYb

I would like to append:

    <thumbnail preview="This is Preview" previewURL="This is URL" thumb="This is Thumb">
        <title><![CDATA[This is Title]]></title>
        <discription><![CDATA[This is Description]]></discription>
    </thumbnail>

To the XML file, with PHP.

I have started out with this:

$xmldoc = new DOMDocument();
$xmldoc->load('/theme1157/sample.xml');

to load the sample.xml.

Can someone show me how I can create a <thumbnail> element in the file and add parameter + values to it like the above example?

Karem
  • 17,615
  • 72
  • 178
  • 278

2 Answers2

2

Try this:

<?php
$xmldoc = new DOMDocument();
$xmldoc->load('sample.xml');



$thumbnail = $xmldoc->createElement('thumbnail');
$thumbnail->setAttribute('preview', 'This is a preview');
$thumbnail->setAttribute('previewURL', 'This is a URL');
$thumbnail->setAttribute('thumb', 'This is a Thumb');

$title = $xmldoc->createElement('title');
$title->appendChild($xmldoc->createCDATASection('This is Title'));
$thumbnail->appendChild($title);


$description = $xmldoc->createElement('description');
$description->appendChild($xmldoc->createCDATASection('This is Description'));
$thumbnail->appendChild($description);


$xmldoc->getElementsByTagName('thumbnails')->item(0)->appendChild($thumbnail);
$xmldoc->save('sample.xml');
k.parnell
  • 2,617
  • 2
  • 16
  • 12
  • Hey! This works almost! The < in title and description turns out as > and the whole element is outside , so it is: ELEMENT IS HERE See http://pastebin.com/XzaRGsX4 – Karem Oct 05 '11 at 18:13
  • Thank you.. Please see this http://stackoverflow.com/questions/7666473/prepending-data-to-xml-file – Karem Oct 05 '11 at 19:17
1

Pretty much the same as DOM manipulation in JavaScript - it should be something like...

$xmlThumbnail = $xmlDoc->createElement('thumbnail');
$xmlTitle = $xmlDoc->createElement('title');
$xmlDescription = $xmlDoc->createElement('discription');

$xmlThumbnail->setAttribute('preview', $sPreview);
$xmlThumbnail->setAttribute('previewurl', $sPreviewURL); //technically in XML attributes must be in lowercase
$xmlThumbnail->setAttribute('thumb', $sThumbnail); //presumably a path?

$xmlThumbnail->appendChild($xmlTitle);
$xmlThumbnail->appendChild($xmlDescription);

$xmlDoc->appendChild($xmlThumbnail);
$xmlDoc->save('/theme1157/sample.xml');

Although the $xmlDoc->appendChild() will probably need changing if it's a "real" XML document as there should be a container element, so it might be something more like $xmlDoc->firstChild->appendChild($xmlThumbnail);

CD001
  • 8,332
  • 3
  • 24
  • 28
  • Not sure that this will work - as XML should only have one root element. – Ed Heal Oct 05 '11 at 17:40
  • Yeah - updated it to add in the `$xmlDoc->firstChild->` bit; though I've seen plenty of "not real" XML documents that just spaff out a load of Elements without a container. – CD001 Oct 05 '11 at 17:42