我正在使用CubeGallery3D,它存储XML文件。
XML结构如下所示: http://pastebin.com/peMxcmYb
我想补充一下:
<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>
使用PHP。到XML文件。
我从这开始:
$xmldoc = new DOMDocument();
$xmldoc->load('/theme1157/sample.xml');
加载sample.xml。
有人可以告诉我如何在文件中创建<thumbnail>
元素并向上面添加参数+值,如上例所示吗?
答案 0 :(得分:2)
试试这个:
<?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');
答案 1 :(得分:0)
与JavaScript中的DOM操作非常相似 - 它应该类似......
$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');
虽然$xmlDoc->appendChild()
可能需要更改,如果它是一个“真正的”XML文档,因为应该有一个容器元素,所以它可能更像是$xmlDoc->firstChild->appendChild($xmlThumbnail);