Page 1 of 1

Few Qs about DOMDocument class

Posted: Wed Feb 24, 2010 11:00 am
by Phix
So I'm not a complete stranger to XML in PHP, but no guru either.

A past project I used the XML DOM to create files, e.g.

Code: Select all

 
$cRoot = $doc->add_root('library');
$cRoot->set_attribute('type', 'contacts');
//etc
 
Now that I've upgraded my WAMP machine to 5.3.1, I'm seeing that the DOMDocument seems to be the way to go now. A site I found (cant remember where now) said the writers of the DOMDocument class made it "verbose" to make sure everything was possible. It just seems like a lot of code for one measly node.

Code: Select all

 
$entryNode = $dom->createElement('entry');
$idNo = $dom->createAttribute('uid');
$rowid = $dom->createAttribute('dbid');
$entryNode->appendChild($rowid);
$entryNode->appendChild($idNo);
//etc
 
Just making sure that's the way the library is... and not my lack of understanding of it.

Second, I can't seem to get it to spit out a complete XML document. Even when I try the uber-basic

Code: Select all

 
$doc = new DOMDocument('1.0');
echo $doc->saveXML();
 
Nothing is returned, but the php.net site says it's supposed to return the <?xml... declaration. I can add nodes and children and all that jazz, but the XML dec is never printed.

Re: Few Qs about DOMDocument class

Posted: Wed Feb 24, 2010 12:24 pm
by requinix
I've yet to hear a story about how nicely DOMDocument creates XML.

Tried creating the XML yourself? htmlspecialchars is your friend.

Code: Select all

echo "<entry uid='123' dbid='456' />";

Re: Few Qs about DOMDocument class

Posted: Wed Feb 24, 2010 12:27 pm
by Phix
It's not bad IMO, just a little heavier than I thought was "needed". Granted, it's extendable so I'll probably end up doing that to keep the main file light on the eyes.

Sure, could do it by hand, but then whats the point of XML libraries? ;)

Re: Few Qs about DOMDocument class

Posted: Wed Feb 24, 2010 1:31 pm
by requinix
Phix wrote:Sure, could do it by hand, but then whats the point of XML libraries? ;)
What's the point of XML libraries that aren't that good for the task at hand?