Few Qs about DOMDocument class

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Phix
Forum Newbie
Posts: 21
Joined: Tue Jan 12, 2010 11:50 pm

Few Qs about DOMDocument class

Post 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.
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Few Qs about DOMDocument class

Post 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' />";
Phix
Forum Newbie
Posts: 21
Joined: Tue Jan 12, 2010 11:50 pm

Re: Few Qs about DOMDocument class

Post 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? ;)
User avatar
requinix
Spammer :|
Posts: 6617
Joined: Wed Oct 15, 2008 2:35 am
Location: WA, USA

Re: Few Qs about DOMDocument class

Post 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?
Post Reply