optikalefx
Programmer
i have an xml document
<pages>
<books>
<title>words</title>
</books>
</pages>
I cant for the life of me figure out how to add a new <books><title>words</title></books> inside of <pages>
so far im able to create the new node and values after the </pages> which throws an error.
so in this first line, how can i get the tag <pages> to use.
<?php
$doc = new DOMDocument();
$doc->preserveWhitespace = false;
$doc->load('xmltest.xml');
// we want a nice output
$xpath = new DOMXPath($doc);
$doc->formatOutput = true;
/* THIS NEEDS TO GET TAG <PAGES> AND STORE IT TO $ROOT */
$root = $doc->createElement('pages');
/* THIS NEEDS TO GET TAG <PAGES> AND STORE IT TO $ROOT */
$root = $doc->appendChild($root);
$inner = $doc->createElement('book');
$inner = $root->appendChild($inner);
$title = $doc->createElement('title');
$title = $inner->appendChild($title);
$text = $doc->createTextNode('This is the title');
$text = $title->appendChild($text);
echo 'Wrote: ' . $doc->save("xmltest.xml") . ' bytes'; // Wrote: 72 bytes
?>
<pages>
<books>
<title>words</title>
</books>
</pages>
I cant for the life of me figure out how to add a new <books><title>words</title></books> inside of <pages>
so far im able to create the new node and values after the </pages> which throws an error.
so in this first line, how can i get the tag <pages> to use.
<?php
$doc = new DOMDocument();
$doc->preserveWhitespace = false;
$doc->load('xmltest.xml');
// we want a nice output
$xpath = new DOMXPath($doc);
$doc->formatOutput = true;
/* THIS NEEDS TO GET TAG <PAGES> AND STORE IT TO $ROOT */
$root = $doc->createElement('pages');
/* THIS NEEDS TO GET TAG <PAGES> AND STORE IT TO $ROOT */
$root = $doc->appendChild($root);
$inner = $doc->createElement('book');
$inner = $root->appendChild($inner);
$title = $doc->createElement('title');
$title = $inner->appendChild($title);
$text = $doc->createTextNode('This is the title');
$text = $title->appendChild($text);
echo 'Wrote: ' . $doc->save("xmltest.xml") . ' bytes'; // Wrote: 72 bytes
?>