Urgent! DomDocument Vs SimpleXML in PHP

Hello All,

I am very new to XML.I have seen various examples where
xml document can be accessed, manipulated,etc.
Some examples using DomDocument like

$xmlDoc = new DOMDocument();

and some examples using SimplXML like

$xml = simplexml_load_file("test.xml");

But can anyone explain me what is the exact difference between these two and which one to use?

I have googled but I didn’t find any solution.

I’ve found that SimpleXML is easier to use when you just need to parse it. If you somehow need to manipulate the XML data then DOM is better. It’s not really a problem because you can easily convert a SimpleXML document to a DOM one instead and vice versa.

Like domDocument (createElement) can we create the element with simpleXML?

Well, there is SimpleXMLElement::addChild(), but as I said above, the DOM classes are much more powerful in that respect (although they might be slightly more complicated to use).

Yeah,you already said that for manipulating the xml domDocument is good and for reading the xml data simpleXML is used. right?
In the link you specified, there is
adding a ‘movie’ child to the parent element.
but we can not create the Parent Element which using simpleXML we can create in domDocument.
is it right?
I want to clear my doubts and to know the concept.


I am not really a PHP programmer but I used this code which works fine on my test server but I get an error when I run on my Web Host server. I came accross this post and now I am wondering if maybe I could get the simpleXML equivalent to this code because my host doesn’t seem to support DOM… I think I need a new web host.

$doc = new DomDocument();

$Contact = $doc->getElementsByTagName('Contact');

foreach($Contact AS $node)
    $name = $node->getAttribute('Name');
    $note = $node->getElementsByTagName('Notes');
	$note1 = $note->item(0);
	$notes = $note1->nodeValue;
    print "<h3>$name</h3>";
    print "<p>$notes</p>";
    print "<hr />";