INDEX

PHP - xml file - create - DOM

PHP XML - create file

(PHP code)
< ?
$dom = new DomDocument('1.0', 'utf-8');
$books = $dom->appendChild($dom->createElement('books'));
$book = $books->appendChild($dom->createElement('book'));
$title = $book->appendChild($dom->createElement('title'));
$title->appendChild($dom->createTextNode('This is sample source.'));
$contents = $book->appendChild($dom->createElement('contents'));
$node= dom_import_simplexml($contents);
$no = $node->ownerDocument;
$node->appendChild($no->createCDATASection('This book is good.'));
$dom->formatOutput = true;
echo $dom->save('test1.xml');
?>
-----------------------------------------
(Result : test1.xml)

< ?xml version="1.0" encoding="utf-8"?>
< books>
  < book>
    < title>This is sample source.< /title>
    < contents>< ![CDATA[This book is good.]]>< /contents>
  < /book>
< /books>
  


Copyright(c) 2007-2023 coding.dojeun.com All Rights Reserved.