Mark Wilson I am the creator of TopXML. I am available for international and local (Australia) contracts. I am a Solution Architect/Business Analyst. I have worked in IT in several countries (NZ, Australia, South Africa, UK) building and training teams for government and very large non-governmental organizations. I am ex-Microsoft Consulting Services. I wrote the first book on Microsoft XML published in 2000 called XML Programming with VB and ASP. Most recently I have been building tools for the SEO industry. Ask me for a 37 point SEO health-checkup for your website.
First posted :
03/24/2008
Times viewed :
349
PHP SimpleXML: Function New_Doc
PHP Version: PHP4 >=
4.2.1
Description
PHPs DOM functions include domxml_new_doc() to create new XML
documents, dmxml_open_file() to open an XML document
file as a DOM object, and domxml_open_mem() to create
a DOM object from an XML document already in memory. These functions return a
DOM object, not a String to other common data type. When you use the PHP DOM
extension functions, you typically first create a DOM document object and then
manipulate it using functions that are part of the class of that object. There
are a number of object classes available, and by starting with a DOMDocument object, you can then examine it and retrieve
new objects reflecting XML document components such as element, attributes and
so on. domxml_new_doc() function creates an XML document from the scratch and
returns it.
Example:
<?php
$doc = domxml_new_doc("1.0");
$root = $doc->create_element("root");
$root = $doc->append_child($root);
$child1 = $doc->create_element("child1");
$child1 = $root->append_child($child1);
$child2= $doc->create_element("child2");
$child2= $head->append_child($child2);
$textchild2 = $doc->create_text_node("Text Element in
Child2");
$ textchild2 = $title->append_child($textchild2);
$doc->dump_file("/tmp/test.xml", false, true);
?>
How
it Works:
I have created a small xml document using functions
available in PHP4.
$doc = domxml_new_doc("1.0");
In the above line of code, I created a new XML DOM
object using domxml_new_doc(). This created a new DOM object.
$root =
$doc->create_element("root");
After I created a new DOM object, I created an root element with name root.
$root =
$doc->append_child($root);
I appended the root element to the DOM document,
which I have created in the first step.
$child1 =
$doc->create_element("child1");
$child1
= $root->append_child($child1);
$child2=
$doc->create_element("child2");
$child2=
$head->append_child($child2);
$textchild2
= $doc->create_text_node("Text Element in Child2");
$
textchild2 = $title->append_child($textchild2);
Then I created some child
elements and text node and appended in appropriate place.
$doc->dump_file("/tmp/test.xml", false, true);
After I created the DOM
document, then I saved it as an xml file with name test.xml.
I used dump_file() function to store as a file.
Warning:
This
function is EXPERIMENTAL. The behavior of
this function, the name of this function, and anything else documented about
this function may change without notice in a future release of PHP. Use this
function at your own risk.